From 10c4b644d2232aeb59d53d4bc1f2c82cdb25ac2c Mon Sep 17 00:00:00 2001 From: traceless Date: Mon, 12 Jul 2021 10:18:51 +0800 Subject: [PATCH 1/4] macos impl --- CoreHtmlToImage/HtmlConverter.cs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/CoreHtmlToImage/HtmlConverter.cs b/CoreHtmlToImage/HtmlConverter.cs index b1cf5e8..8a1bd1b 100644 --- a/CoreHtmlToImage/HtmlConverter.cs +++ b/CoreHtmlToImage/HtmlConverter.cs @@ -38,7 +38,7 @@ 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, @@ -65,7 +65,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"); + } } } From 368cd5254ca9ae8e44a57cda157409cd0dbcb45d Mon Sep 17 00:00:00 2001 From: traceless Date: Mon, 12 Jul 2021 10:54:49 +0800 Subject: [PATCH 2/4] FromHtmlString encoding --- CoreHtmlToImage/HtmlConverter.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/CoreHtmlToImage/HtmlConverter.cs b/CoreHtmlToImage/HtmlConverter.cs index 8a1bd1b..a1666d5 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 { @@ -95,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) { + 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; From 700f001b46f6f4cf0837a013b26b62f1d1a65f8e Mon Sep 17 00:00:00 2001 From: traceless Date: Mon, 12 Jul 2021 17:58:05 +0800 Subject: [PATCH 3/4] Update HtmlConverter.cs --- CoreHtmlToImage/HtmlConverter.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CoreHtmlToImage/HtmlConverter.cs b/CoreHtmlToImage/HtmlConverter.cs index a1666d5..e852bfc 100644 --- a/CoreHtmlToImage/HtmlConverter.cs +++ b/CoreHtmlToImage/HtmlConverter.cs @@ -44,11 +44,11 @@ static HtmlConverter() { 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(); From 97ecc1dff00c43ff77d07a947524bc297d39fbaa Mon Sep 17 00:00:00 2001 From: traceless Date: Mon, 16 Aug 2021 14:53:29 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E7=BC=96=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CoreHtmlToImage.Console/Program.cs | 7 ++++--- CoreHtmlToImage/HtmlConverter.cs | 12 ++++++------ 2 files changed, 10 insertions(+), 9 deletions(-) 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 e852bfc..f39bd27 100644 --- a/CoreHtmlToImage/HtmlConverter.cs +++ b/CoreHtmlToImage/HtmlConverter.cs @@ -101,7 +101,7 @@ static HtmlConverter() /// Output image format /// Output image quality 1-100 /// - public byte[] FromHtmlString(string html,Encoding encoding=null, 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) { @@ -122,7 +122,7 @@ public byte[] FromHtmlString(string html,Encoding encoding=null, int width = 102 /// 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}"); @@ -131,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