diff --git a/CoreHtmlToImage.Console/Program.cs b/CoreHtmlToImage.Console/Program.cs
index 6d6660c..6ece390 100644
--- a/CoreHtmlToImage.Console/Program.cs
+++ b/CoreHtmlToImage.Console/Program.cs
@@ -1,4 +1,5 @@
using System.IO;
+using System.Text;
namespace CoreHtmlToImage.Console
{
@@ -9,11 +10,11 @@ static void Main(string[] args)
// From HTML string
var converter = new HtmlConverter();
var html = "
Hello World!
";
- var htmlBytes = converter.FromHtmlString(html);
+ var htmlBytes = converter.FromHtmlString(html,Encoding.UTF8);
// From URL
- var urlBytes = converter.FromUrl("http://google.com", 800, format: ImageFormat.Png, quality: 90);
- File.WriteAllBytes("D:\\image.png", urlBytes);
+ var urlBytes = converter.FromUrl("http://baidu.com", 800, format: ImageFormat.Png, quality: 90);
+ File.WriteAllBytes("/Users/traceless/image.png", urlBytes);
}
}
}
diff --git a/CoreHtmlToImage/HtmlConverter.cs b/CoreHtmlToImage/HtmlConverter.cs
index b1cf5e8..f39bd27 100644
--- a/CoreHtmlToImage/HtmlConverter.cs
+++ b/CoreHtmlToImage/HtmlConverter.cs
@@ -2,6 +2,7 @@
using System.Diagnostics;
using System.IO;
using System.Reflection;
+using System.Text;
namespace CoreHtmlToImage
{
@@ -38,16 +39,16 @@ static HtmlConverter()
}
else if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Linux))
{
- //Check if wkhtmltoimage package is installed on this distro in using which command
+ //Check if wkhtmltoimage package is installed in using which command
Process process = Process.Start(new ProcessStartInfo()
{
CreateNoWindow = true,
UseShellExecute = false,
- WorkingDirectory = "/bin/",
+ WorkingDirectory = "/usr/local/bin",
RedirectStandardOutput = true,
RedirectStandardError = true,
- FileName = "/bin/bash",
- Arguments = "which wkhtmltoimage"
+ FileName = "which",
+ Arguments = "wkhtmltoimage"
});
string answer = process.StandardOutput.ReadToEnd();
@@ -65,7 +66,29 @@ static HtmlConverter()
else
{
//OSX not implemented
- throw new Exception("OSX Platform not implemented yet");
+ //Check if wkhtmltoimage package is installed on this distro in using which command
+ Process process = Process.Start(new ProcessStartInfo()
+ {
+ CreateNoWindow = true,
+ UseShellExecute = false,
+ WorkingDirectory = "/usr/local/bin",
+ RedirectStandardOutput = true,
+ RedirectStandardError = true,
+ FileName = "which",
+ Arguments = "wkhtmltoimage"
+
+ });
+ string answer = process.StandardOutput.ReadToEnd();
+ process.WaitForExit();
+
+ if (!string.IsNullOrEmpty(answer) && answer.Contains("wkhtmltoimage"))
+ {
+ toolFilepath = "wkhtmltoimage";
+ }
+ else
+ {
+ throw new Exception("wkhtmltoimage does not appear to be installed on MacOS according to which command; go to https://wkhtmltopdf.org/downloads.html");
+ }
}
}
@@ -73,14 +96,19 @@ static HtmlConverter()
/// Converts HTML string to image
///
/// HTML string
+ /// string encoding
/// Output document width
/// Output image format
/// Output image quality 1-100
///
- public byte[] FromHtmlString(string html, int width = 1024, ImageFormat format = ImageFormat.Jpg, int quality = 100)
+ public byte[] FromHtmlString(string html,Encoding encoding=null, int width = 1024, ImageFormat format = ImageFormat.Jpg, int quality = 100,string extraParas="")
{
+ if (null == encoding)
+ {
+ encoding=Encoding.Default;
+ }
var filename = Path.Combine(directory, $"{Guid.NewGuid()}.html");
- File.WriteAllText(filename, html);
+ File.WriteAllText(filename, html,encoding);
var bytes = FromUrl(filename, width, format, quality);
File.Delete(filename);
return bytes;
@@ -94,7 +122,7 @@ public byte[] FromHtmlString(string html, int width = 1024, ImageFormat format =
/// Output image format
/// Output image quality 1-100
///
- public byte[] FromUrl(string url, int width = 1024, ImageFormat format = ImageFormat.Jpg, int quality = 100)
+ public byte[] FromUrl(string url, int width = 1024, ImageFormat format = ImageFormat.Jpg, int quality = 100,string extraParas="")
{
var imageFormat = format.ToString().ToLower();
var filename = Path.Combine(directory, $"{Guid.NewGuid().ToString()}.{imageFormat}");
@@ -103,17 +131,17 @@ public byte[] FromUrl(string url, int width = 1024, ImageFormat format = ImageFo
if (IsLocalPath(url))
{
- args = $"--quality {quality} --width {width} -f {imageFormat} \"{url}\" \"{filename}\"";
+ args = $"{extraParas} --quality {quality} --width {width} -f {imageFormat} \"{url}\" \"{filename}\"";
}
else
{
- args = $"--quality {quality} --width {width} -f {imageFormat} {url} \"{filename}\"";
+ args = $"{extraParas} --quality {quality} --width {width} -f {imageFormat} {url} \"{filename}\"";
}
Process process = Process.Start(new ProcessStartInfo(toolFilepath, args)
{
- WindowStyle = ProcessWindowStyle.Hidden,
- CreateNoWindow = true,
+ WindowStyle = ProcessWindowStyle.Normal,
+ CreateNoWindow = false,
UseShellExecute = false,
WorkingDirectory = directory,
RedirectStandardError = true