ln.templates/ln.templates.service/render/HtmlToPDF.cs

122 lines
4.1 KiB
C#

using System;
using System.Diagnostics;
using System.IO;
using System.Text;
namespace ln.templates.service.render
{
public class HtmlToPDF
{
public static string WkExecutablePath { get; set; }
public static string ChromiumExecutablePath { get; set; }
public static bool RenderPDF(Stream htmlStream, string resourcePath, out Stream pdfStream)
{
if (htmlStream is TempFileStream tempFileStream)
{
return RenderPDF(tempFileStream.Name, resourcePath, out pdfStream);
}
else
{
using (TempFileStream htmlTempStream = new TempFileStream(".html"))
{
htmlStream.CopyTo(htmlTempStream);
return RenderPDF(htmlTempStream.Name, resourcePath, out pdfStream);
}
}
}
public static bool RenderPDF(string htmlFileName, string resourcePath, out Stream pdfStream)
{
if (ChromiumExecutablePath is string)
return RenderPDFChromium(htmlFileName, resourcePath, out pdfStream);
else
return RenderPDFWk(htmlFileName, resourcePath, out pdfStream);
}
public static bool RenderPDFWk(string htmlFileName, string resourcePath, out Stream pdfStream)
{
if (!File.Exists(htmlFileName))
throw new FileNotFoundException(htmlFileName);
if ((resourcePath is String) && !Directory.Exists(resourcePath))
throw new DirectoryNotFoundException(resourcePath);
var pdfTempStream = new TempFileStream(".pdf");
StringBuilder argumentsBuilder = new StringBuilder();
if (resourcePath is String)
argumentsBuilder.AppendFormat("--allow {0} ", resourcePath);
argumentsBuilder.Append("--disable-javascript --enable-local-file-access -B 0 -L 0 -R 0 -T 0 --print-media-type");
argumentsBuilder.AppendFormat(" {0}", htmlFileName);
argumentsBuilder.AppendFormat(" {0}", pdfTempStream.Name);
Console.WriteLine("Starting WkHtmlToPdf: {0} {1}", WkExecutablePath, argumentsBuilder.ToString());
var p = Process.Start(WkExecutablePath, argumentsBuilder.ToString());
p.WaitForExit();
if (p.ExitCode != 0)
{
pdfTempStream.Dispose();
pdfStream = null;
return false;
}
pdfStream = pdfTempStream;
return true;
}
public static bool RenderPDFChromium(string htmlFileName, string resourcePath, out Stream pdfStream)
{
var pdfTempStream = new TempFileStream(".pdf");
string command =
String.Format(
"--headless=old --disable-gpu --print-to-pdf={1} --no-pdf-header-footer --no-margins {0}",
htmlFileName,
pdfTempStream.Name
);
Console.WriteLine("Starting chromium: {0} {1}", ChromiumExecutablePath, command);
var p = Process.Start(ChromiumExecutablePath, command);
p.WaitForExit();
if (p.ExitCode != 0)
{
pdfTempStream.Dispose();
pdfStream = null;
return false;
}
pdfStream = pdfTempStream;
return true;
}
static string LookupExecutable(String[] searchPaths)
{
foreach (var tryExe in searchPaths)
{
if (File.Exists(tryExe))
return tryExe;
}
return null;
}
static HtmlToPDF()
{
WkExecutablePath = LookupExecutable(new String[]
{
"/usr/bin/wkhtmltopdf",
"/usr/local/bin/wkhtmltopdf",
});
ChromiumExecutablePath = LookupExecutable(new String[]
{
"/usr/bin/chromium",
"/usr/local/bin/chromium",
"/usr/bin/chrome",
"/usr/local/bin/chrome",
});
}
}
}