This repository has been archived by the owner on Dec 14, 2022. It is now read-only.
/
WkhtmlDriver.cs
133 lines (116 loc) · 4.34 KB
/
WkhtmlDriver.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
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace Wkhtmltopdf.NetCore
{
public abstract class WkhtmlDriver
{
/// <summary>
/// Converts given URL or HTML string to PDF.
/// </summary>
/// <param name="wkhtmlPath">Path to wkthmltopdf\wkthmltoimage.</param>
/// <param name="switches">Switches that will be passed to wkhtmltopdf binary.</param>
/// <param name="html">String containing HTML code that should be converted to PDF.</param>
/// <returns>PDF as byte array.</returns>
public static byte[] Convert(string wkhtmlPath, string switches, string html)
{
// switches:
// "-q" - silent output, only errors - no progress messages
// " -" - switch output to stdout
// "- -" - switch input to stdin and output to stdout
switches = "-q " + switches + " -";
// generate PDF from given HTML string, not from URL
if (!string.IsNullOrEmpty(html))
{
switches += " -";
html = SpecialCharsEncode(html);
}
string rotativaLocation;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
rotativaLocation = Path.Combine(wkhtmlPath, "Windows", "wkhtmltopdf.exe");
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
rotativaLocation = Path.Combine(wkhtmlPath, "Mac", "wkhtmltopdf");
}
else
{
rotativaLocation = Path.Combine(wkhtmlPath, "Linux", "wkhtmltopdf");
}
if (!File.Exists(rotativaLocation))
{
throw new Exception("wkhtmltopdf not found, searched for " + rotativaLocation);
}
using (var proc = new Process())
{
try
{
proc.StartInfo = new ProcessStartInfo
{
FileName = rotativaLocation,
Arguments = switches,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
CreateNoWindow = true
};
proc.Start();
}
catch (Exception ex)
{
throw ex;
}
// generate PDF from given HTML string, not from URL
if (!string.IsNullOrEmpty(html))
{
using (var sIn = proc.StandardInput)
{
sIn.WriteLine(html);
}
}
using (var ms = new MemoryStream())
{
using (var sOut = proc.StandardOutput.BaseStream)
{
byte[] buffer = new byte[4096];
int read;
while ((read = sOut.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
}
string error = proc.StandardError.ReadToEnd();
if (ms.Length == 0)
{
throw new Exception(error);
}
proc.WaitForExit();
return ms.ToArray();
}
}
}
/// <summary>
/// Encode all special chars
/// </summary>
/// <param name="text">Html text</param>
/// <returns>Html with special chars encoded</returns>
private static string SpecialCharsEncode(string text)
{
var chars = text.ToCharArray();
var result = new StringBuilder(text.Length + (int)(text.Length * 0.1));
foreach (var c in chars)
{
var value = System.Convert.ToInt32(c);
if (value > 127)
result.AppendFormat("&#{0};", value);
else
result.Append(c);
}
return result.ToString();
}
}
}