Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions CoreHtmlToImage.Console/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System.Text;

namespace CoreHtmlToImage.Console
{
Expand All @@ -9,11 +10,11 @@ static void Main(string[] args)
// From HTML string
var converter = new HtmlConverter();
var html = "<div><strong>Hello</strong> World!</div>";
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);
}
}
}
52 changes: 40 additions & 12 deletions CoreHtmlToImage/HtmlConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;

namespace CoreHtmlToImage
{
Expand Down Expand Up @@ -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();
Expand All @@ -65,22 +66,49 @@ 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");
}
}
}

/// <summary>
/// Converts HTML string to image
/// </summary>
/// <param name="html">HTML string</param>
/// <param name="encoding">string encoding</param>
/// <param name="width">Output document width</param>
/// <param name="format">Output image format</param>
/// <param name="quality">Output image quality 1-100</param>
/// <returns></returns>
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;
Expand All @@ -94,7 +122,7 @@ public byte[] FromHtmlString(string html, int width = 1024, ImageFormat format =
/// <param name="format">Output image format</param>
/// <param name="quality">Output image quality 1-100</param>
/// <returns></returns>
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}");
Expand All @@ -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
Expand Down