Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added options 'landscape' and 'format' #81

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
};
}
}