Skip to content

Commit

Permalink
Merge pull request #60 from by-pinja/feature/pdf-options
Browse files Browse the repository at this point in the history
Added support for pdf options
  • Loading branch information
samikaur authored Apr 30, 2020
2 parents 955a87c + d1b72ba commit e267d0e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
20 changes: 20 additions & 0 deletions ApiDescription.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,26 @@ Real example:

### List of translators and options

#### pdf options

Pdf can contain following options

```json
{
"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,
"preferCSSPageSize": false,
"pageRanges": null,
"marginTop": "120px",
"marginBottom": "120px",
"marginLeft": "20px",
"marginRight": "20px"
}
```

For further information, see [https://www.puppeteersharp.com/api/PuppeteerSharp.PdfOptions.html](https://www.puppeteersharp.com/api/PuppeteerSharp.PdfOptions.html).

#### barcode

Generates a barcode image of type `code128|...`
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=79.0.3945.88-r0
ARG chromium_version=81.0.4044.113-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 @@ -52,7 +52,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.0" />
<PackageReference Include="PuppeteerSharp" Version="2.0.3" />

<!-- Remove this once testing library is updated -->
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.1.0" />
Expand Down
29 changes: 26 additions & 3 deletions Pdf/PdfQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.Extensions.Options;
using PuppeteerSharp.Media;
using System.Text;
using Newtonsoft.Json.Linq;

namespace Pdf.Storage.Pdf
{
Expand Down Expand Up @@ -38,7 +39,8 @@ public void CreatePdf(Guid pdfEntityId)
{
var entity = _context.PdfFiles.Single(x => x.Id == pdfEntityId);
var htmlFromStorage = _storage.Get(new StorageFileId(entity, "html"));
var data = GeneratePdfDataFromHtml(pdfEntityId, Encoding.UTF8.GetString(htmlFromStorage.Data)).GetAwaiter().GetResult();
var data = GeneratePdfDataFromHtml(pdfEntityId, Encoding.UTF8.GetString(htmlFromStorage.Data),
entity.Options).GetAwaiter().GetResult();

_storage.AddOrReplace(new StorageData(new StorageFileId(entity), data));

Expand All @@ -49,7 +51,7 @@ public void CreatePdf(Guid pdfEntityId)
_context.SaveChanges();
}

private async Task<byte[]> GeneratePdfDataFromHtml(Guid id, string html)
private async Task<byte[]> GeneratePdfDataFromHtml(Guid id, string html, JObject options)
{
_logger.LogDebug($"Generating pdf from {id}");

Expand All @@ -76,7 +78,28 @@ await page.SetContentAsync(html,
WaitUntil = new[] { WaitUntilNavigation.Load, WaitUntilNavigation.DOMContentLoaded }
});

return await page.PdfDataAsync(new PdfOptions { Format = PaperFormat.A4 });
var defaultPdfOptions = new PdfOptions
{
Format = PaperFormat.A4
};

return await page.PdfDataAsync(new PdfOptions
{
Format = 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,
PreferCSSPageSize = options.ContainsKey("preferCSSPageSize") ? options["preferCSSPageSize"].Value<bool>() : defaultPdfOptions.PreferCSSPageSize,
PageRanges = options.ContainsKey("pageRanges") ? options["pageRanges"].Value<string>() : defaultPdfOptions.PageRanges,
MarginOptions = new MarginOptions
{
Top = options.ContainsKey("marginTop") ? options["marginTop"].Value<string>() : defaultPdfOptions.MarginOptions.Top,
Bottom = options.ContainsKey("marginBottom") ? options["marginBottom"].Value<string>() : defaultPdfOptions.MarginOptions.Bottom,
Left = options.ContainsKey("marginLeft") ? options["marginLeft"].Value<string>() : defaultPdfOptions.MarginOptions.Left,
Right = options.ContainsKey("marginRight") ? options["marginRight"].Value<string>() : defaultPdfOptions.MarginOptions.Right,
}
});
}
catch (Exception e)
{
Expand Down

0 comments on commit e267d0e

Please sign in to comment.