Add WeasyPrint to convert PDFs
This commit is contained in:
@ -1,64 +1,45 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using PdfSharp.Pdf.IO;
|
||||
using PuppeteerSharp;
|
||||
using Elwig.Helpers;
|
||||
using Elwig.Windows;
|
||||
using System.Diagnostics;
|
||||
using Balbarak.WeasyPrint;
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace Elwig.Documents {
|
||||
public static class Pdf {
|
||||
|
||||
private static readonly string Chromium = @"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";
|
||||
private static readonly string PdfToPrinter = App.ExePath + "PDFtoPrinter.exe";
|
||||
private static IBrowser? Browser = null;
|
||||
public static bool IsReady => Browser != null;
|
||||
private static readonly FilesManager WeasyPrintManager = new();
|
||||
private static string? WeasyPrintPython = null;
|
||||
private static string? WeasyPrintDir => WeasyPrintManager.FolderPath;
|
||||
public static bool IsReady => WeasyPrintPython != null && WeasyPrintDir != null;
|
||||
|
||||
public static async Task Init(Action evtHandler) {
|
||||
Browser = await Puppeteer.LaunchAsync(new LaunchOptions {
|
||||
Headless = true,
|
||||
ExecutablePath = Chromium,
|
||||
// paged.js uses XHRs to load styles, so this is needed
|
||||
Args = new[] { "--allow-file-access-from-files" },
|
||||
});
|
||||
if (!WeasyPrintManager.IsFilesExsited()) {
|
||||
await WeasyPrintManager.InitFilesAsync();
|
||||
}
|
||||
WeasyPrintPython = Path.Combine(WeasyPrintManager.FolderPath, "python.exe");
|
||||
evtHandler();
|
||||
}
|
||||
|
||||
public static async Task Close() {
|
||||
if (Browser == null) return;
|
||||
await Browser.CloseAsync();
|
||||
Browser = null;
|
||||
}
|
||||
|
||||
public static async Task Convert(string htmlPath, string pdfPath) {
|
||||
if (Browser == null) throw new InvalidOperationException("The puppeteer engine has not been initialized yet");
|
||||
using var page = await Browser.NewPageAsync();
|
||||
page.Console += OnConsole;
|
||||
await page.GoToAsync($"file://{htmlPath}");
|
||||
await page.EvaluateFunctionAsync("async () => { await window.PagedPolyfill.preview(); }");
|
||||
await page.PdfAsync(pdfPath, new() {
|
||||
PreferCSSPageSize = true,
|
||||
//Format = PaperFormat.A4,
|
||||
DisplayHeaderFooter = false,
|
||||
MarginOptions = new() {
|
||||
Top = "0mm",
|
||||
Right = "0mm",
|
||||
Bottom = "0mm",
|
||||
Left = "0mm",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
private static void OnConsole(object? sender, ConsoleEventArgs e) {
|
||||
MessageBox.Show(e.Message.Text);
|
||||
}
|
||||
|
||||
public static void UpdateMetadata(string path, string title, string author) {
|
||||
using var doc = PdfReader.Open(path);
|
||||
doc.Info.Title = title;
|
||||
doc.Info.Author = author;
|
||||
doc.Save(path);
|
||||
var p = new Process() { StartInfo = new() {
|
||||
FileName = WeasyPrintPython,
|
||||
CreateNoWindow = true,
|
||||
WorkingDirectory = WeasyPrintDir,
|
||||
RedirectStandardError = true,
|
||||
} };
|
||||
p.StartInfo.EnvironmentVariables["PATH"] = "Scripts;gtk3;" + Environment.GetEnvironmentVariable("PATH");
|
||||
p.StartInfo.ArgumentList.Add("scripts/weasyprint.exe");
|
||||
p.StartInfo.ArgumentList.Add("-e");
|
||||
p.StartInfo.ArgumentList.Add("utf8");
|
||||
p.StartInfo.ArgumentList.Add(htmlPath);
|
||||
p.StartInfo.ArgumentList.Add(pdfPath);
|
||||
p.Start();
|
||||
await p.WaitForExitAsync();
|
||||
var stderr = await p.StandardError.ReadToEndAsync();
|
||||
if (p.ExitCode != 0) throw new Exception(stderr);
|
||||
}
|
||||
|
||||
public static void Show(TempFile file, string title) {
|
||||
|
Reference in New Issue
Block a user