diff --git a/src/Weasyprint.Wrapped/Printer.cs b/src/Weasyprint.Wrapped/Printer.cs index fff49b8..79bee39 100644 --- a/src/Weasyprint.Wrapped/Printer.cs +++ b/src/Weasyprint.Wrapped/Printer.cs @@ -88,6 +88,29 @@ public async Task Print(string html, CancellationToken cancellation ); } + /// + /// Prints the given html to pdf using the weasyprint library. + /// + /// html file name with path to be converted to pdf + /// pdf file name with path for output + /// Optional cancellationToken, passed to the executing command + /// list of additional parameter for weasyprint (see readme.md#Weasyprint-CLI) + public async Task Print(string htmlFile, string pdfFile, CancellationToken cancellationToken = default, params string[] additionalParameters) + { + var stdErrBuffer = new StringBuilder(); + var result = await BuildOsSpecificCommand() + .WithArguments($"-m weasyprint --encoding utf8 --base-url {baseUrl} {string.Join(" ", additionalParameters)} {htmlFile} {pdfFile}") + .WithStandardErrorPipe(PipeTarget.ToStringBuilder(stdErrBuffer)) + .WithValidation(CommandResultValidation.None) + .ExecuteBufferedAsync(Encoding.UTF8, cancellationToken); + return new PrintResult( + Array.Empty(), + stdErrBuffer.ToString(), + result.RunTime, + result.ExitCode + ); + } + private Command BuildOsSpecificCommand() { Command command;