-
Notifications
You must be signed in to change notification settings - Fork 54
/
HtmlConverter.cs
150 lines (132 loc) · 5.47 KB
/
HtmlConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
namespace CoreHtmlToImage
{
/// <summary>
/// Html Converter. Converts HTML string and URLs to image bytes
/// </summary>
public class HtmlConverter
{
private static string toolFilename = "wkhtmltoimage";
private static string directory;
private static string toolFilepath;
static HtmlConverter()
{
directory = AppContext.BaseDirectory;
//Check on what platform we are
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
{
toolFilepath = Path.Combine(directory, toolFilename + ".exe");
if (!File.Exists(toolFilepath))
{
var assembly = typeof(HtmlConverter).GetTypeInfo().Assembly;
var type = typeof(HtmlConverter);
var ns = type.Namespace;
using (var resourceStream = assembly.GetManifestResourceStream($"{ns}.{toolFilename}.exe"))
using (var fileStream = File.OpenWrite(toolFilepath))
{
resourceStream.CopyTo(fileStream);
}
}
}
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
Process process = Process.Start(new ProcessStartInfo()
{
CreateNoWindow = true,
UseShellExecute = false,
WorkingDirectory = "/bin/",
RedirectStandardOutput = true,
RedirectStandardError = true,
FileName = "/bin/bash",
Arguments = "which 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 this linux system according to which command; go to https://wkhtmltopdf.org/downloads.html");
}
}
else
{
//OSX not implemented
throw new Exception("OSX Platform not implemented yet");
}
}
/// <summary>
/// Converts HTML string to image
/// </summary>
/// <param name="html">HTML string</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)
{
var filename = Path.Combine(directory, $"{Guid.NewGuid()}.html");
File.WriteAllText(filename, html);
var bytes = FromUrl(filename, width, format, quality);
File.Delete(filename);
return bytes;
}
/// <summary>
/// Converts HTML page to image
/// </summary>
/// <param name="url">Valid http(s):// URL</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[] FromUrl(string url, int width = 1024, ImageFormat format = ImageFormat.Jpg, int quality = 100)
{
var imageFormat = format.ToString().ToLower();
var filename = Path.Combine(directory, $"{Guid.NewGuid().ToString()}.{imageFormat}");
string args;
if (IsLocalPath(url))
{
args = $"--quality {quality} --width {width} -f {imageFormat} \"{url}\" \"{filename}\"";
}
else
{
args = $"--quality {quality} --width {width} -f {imageFormat} {url} \"{filename}\"";
}
Process process = Process.Start(new ProcessStartInfo(toolFilepath, args)
{
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true,
UseShellExecute = false,
WorkingDirectory = directory,
RedirectStandardError = true
});
process.ErrorDataReceived += Process_ErrorDataReceived;
process.WaitForExit();
if (File.Exists(filename))
{
var bytes = File.ReadAllBytes(filename);
File.Delete(filename);
return bytes;
}
throw new Exception("Something went wrong. Please check input parameters");
}
private static bool IsLocalPath(string path)
{
if (path.StartsWith("http"))
{
return false;
}
return new Uri(path).IsFile;
}
private void Process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
throw new Exception(e.Data);
}
}
}