Skip to content

Commit

Permalink
Merge pull request #48 from AndreiMikhniuk/main
Browse files Browse the repository at this point in the history
Allows to call weasyprint with file paths
  • Loading branch information
berthertogen authored Dec 30, 2023
2 parents 2bf70ee + e1ca35b commit 163d7f4
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Weasyprint.Wrapped/Printer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,29 @@ public async Task<PrintResult> Print(string html, CancellationToken cancellation
);
}

/// <summary>
/// Prints the given html to pdf using the weasyprint library.
/// </summary>
/// <param name="htmlFile">html file name with path to be converted to pdf</param>
/// <param name="pdfFile">pdf file name with path for output</param>
/// <param name="cancellationToken">Optional cancellationToken, passed to the executing command</param>
/// <param name="additionalParameters">list of additional parameter for weasyprint (see readme.md#Weasyprint-CLI)</param>
public async Task<PrintResult> 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<byte>(),
stdErrBuffer.ToString(),
result.RunTime,
result.ExitCode
);
}

private Command BuildOsSpecificCommand()
{
Command command;
Expand Down

0 comments on commit 163d7f4

Please sign in to comment.