Skip to content

Commit

Permalink
Added options 'landscape' and 'format' (#81)
Browse files Browse the repository at this point in the history
* Added landscape option

* Added PaperFormat format option
  • Loading branch information
KimmoLappalainen authored Dec 2, 2024
1 parent a7a3b37 commit 0087ca6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 3 additions & 1 deletion ApiDescription.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ Pdf can contain following options

```json
{
"format": "A4",
"footerTemplate": "<div style=\"color: black; font-size: 12px; width: 100%; margin-left: 28px;\"><span class=\"pageNumber\"></span>/<span class=\"totalPages\"></span></div>",
"headerTemplate": "<div style=\"color: black; font-size: 12px; width: 100%; margin-left: 28px;\">Some header</div>",
"printBackground": true,
"landscape": false,
"preferCSSPageSize": false,
"pageRanges": null,
"marginTop": "120px",
Expand All @@ -56,6 +58,6 @@ Pdf can contain following options
"scale": null
}
```
Values in width AND height (in inches) creates a custom sized paper. If omitted the default A4 paper size will be used.
Format takes priority over width and height values. Values in width AND height (in inches) creates a custom sized paper. If format and size params are omitted the default A4 paper size will be used.

For further information, see [https://www.puppeteersharp.com/api/PuppeteerSharp.PdfOptions.html](https://www.puppeteersharp.com/api/PuppeteerSharp.PdfOptions.html).
18 changes: 17 additions & 1 deletion Pdf/PdfQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,12 @@ await page.SetContentAsync(html,

return await page.PdfDataAsync(new PdfOptions
{
Format = defaultPdfOptions.Format,
Format = options.ContainsKey("format") ? Format(options["format"].Value<string>()) : defaultPdfOptions.Format,
DisplayHeaderFooter = options.ContainsKey("footerTemplate") || options.ContainsKey("headerTemplate"),
FooterTemplate = options.ContainsKey("footerTemplate") ? options["footerTemplate"].Value<string>() : defaultPdfOptions.FooterTemplate,
HeaderTemplate = options.ContainsKey("headerTemplate") ? options["headerTemplate"].Value<string>() : defaultPdfOptions.HeaderTemplate,
PrintBackground = options.ContainsKey("printBackground") ? options["printBackground"].Value<bool>() : defaultPdfOptions.PrintBackground,
Landscape = options.ContainsKey("landscape") ? options["landscape"].Value<bool>() : defaultPdfOptions.Landscape,
PreferCSSPageSize = options.ContainsKey("preferCSSPageSize") ? options["preferCSSPageSize"].Value<bool>() : defaultPdfOptions.PreferCSSPageSize,
PageRanges = options.ContainsKey("pageRanges") ? options["pageRanges"].Value<string>() : defaultPdfOptions.PageRanges,
Scale = options.ContainsKey("scale") ? options["scale"].Value<decimal>() : defaultPdfOptions.Scale,
Expand All @@ -129,5 +130,20 @@ await page.SetContentAsync(html,
browser?.Dispose();
}
}

private static PaperFormat Format(string format) => format switch {
"Letter" => PaperFormat.Letter,
"Legal" => PaperFormat.Legal,
"Tabloid" => PaperFormat.Tabloid,
"Ledger" => PaperFormat.Ledger,
"A0" => PaperFormat.A0,
"A1" => PaperFormat.A1,
"A2" => PaperFormat.A2,
"A3" => PaperFormat.A3,
"A4" => PaperFormat.A4,
"A5" => PaperFormat.A5,
"A6" => PaperFormat.A6,
_ => PaperFormat.A4,
};
}
}

0 comments on commit 0087ca6

Please sign in to comment.