Skip to content

Commit

Permalink
Setting a custom PaperFormat with Width/Height options (#67)
Browse files Browse the repository at this point in the history
* Setting a custom PaperFormat with Width/Height options

* Updated puppeteer and chromium

* Added width and height to pdf options

Co-authored-by: Savpek <savolainen.pekka@gmail.com>
  • Loading branch information
juha-heikkinen and savpek authored Jun 28, 2021
1 parent b5ac3e1 commit ae967af
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
5 changes: 4 additions & 1 deletion ApiDescription.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,12 @@ Pdf can contain following options
"marginTop": "120px",
"marginBottom": "120px",
"marginLeft": "20px",
"marginRight": "20px"
"marginRight": "20px",
"width": null,
"height": null
}
```
Values in width AND height (in inches) creates a custom sized paper. If 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).

Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ! IMPORTANT: Keep chromium version synced with version from package 'PuppeteerSharp'
# and match it with from https://pkgs.alpinelinux.org/packages
ARG chromium_version=89.0.4389.128-r2
ARG chromium_version=91.0.4472.101-r0

FROM mcr.microsoft.com/dotnet/core/sdk:3.1.100-alpine3.10 as dotnetBuild
ARG chromium_version
Expand Down
2 changes: 1 addition & 1 deletion Pdf.Storage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<!-- When you update PuppeteerSharp you must also find corresponding version
in alpine at Dockerfile. Alpine limits available chromium versions available so don't update
this without check them out. Chromium version must be about same as puppeteer expects -->
<PackageReference Include="PuppeteerSharp" Version="2.0.3" />
<PackageReference Include="PuppeteerSharp" Version="4.0.0" />

<!-- Remove this once testing library is updated -->
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.1.0" />
Expand Down
7 changes: 6 additions & 1 deletion Pdf/PdfQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ await page.SetContentAsync(html,

var defaultPdfOptions = new PdfOptions
{
Format = PaperFormat.A4
Format = options.ContainsKey("Width") && options.ContainsKey("Height") ?
new PaperFormat(
options["Width"].Value<decimal>(),
options["Height"].Value<decimal>()
) :
PaperFormat.A4
};

return await page.PdfDataAsync(new PdfOptions
Expand Down

0 comments on commit ae967af

Please sign in to comment.