<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Frank</title>
    <description>The latest articles on DEV Community by Frank (@frank_32f82b4e249028949b9).</description>
    <link>https://dev.to/frank_32f82b4e249028949b9</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4184054%2F3aef3bbf-ed16-4f91-9c52-612857afa897.jpg</url>
      <title>DEV Community: Frank</title>
      <link>https://dev.to/frank_32f82b4e249028949b9</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/frank_32f82b4e249028949b9"/>
    <language>en</language>
    <item>
      <title>Same invoice, four .NET HTML to PDF libraries: what broke</title>
      <dc:creator>Frank</dc:creator>
      <pubDate>Thu, 26 Nov 2026 09:42:56 +0000</pubDate>
      <link>https://dev.to/frank_32f82b4e249028949b9/same-invoice-four-net-html-to-pdf-libraries-what-broke-2cih</link>
      <guid>https://dev.to/frank_32f82b4e249028949b9/same-invoice-four-net-html-to-pdf-libraries-what-broke-2cih</guid>
      <description>&lt;p&gt;Ask how to turn HTML into a PDF in C# and you get a list of library names. What you rarely get is the same document run through each of them, so you can see what comes out. So we did that: one invoice, four open-source .NET libraries, each set up the way its own documentation describes, on the kind of Linux machine these things end up running on.&lt;/p&gt;

&lt;p&gt;Disclosure: I'm on the team behind &lt;a href="https://dynamicdocumentapi.com/" rel="noopener noreferrer"&gt;Dynamic Document API&lt;/a&gt;, a hosted HTML to PDF API, so we have a horse in this race. That's why everything below is in a &lt;a href="https://github.com/DynamicDocumentApi/html-to-pdf-csharp/tree/main/compare" rel="noopener noreferrer"&gt;public repository&lt;/a&gt; and runs in GitHub Actions: the code, the PDFs and the run log. Commercial libraries aren't part of the test.&lt;/p&gt;

&lt;h2&gt;
  
  
  The test document
&lt;/h2&gt;

&lt;p&gt;Nothing exotic, just what a real invoice uses today:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a header laid out with &lt;strong&gt;Flexbox&lt;/strong&gt;, with an &lt;strong&gt;inline SVG&lt;/strong&gt; logo&lt;/li&gt;
&lt;li&gt;the invoice details (number, dates, PO) in a two-column &lt;strong&gt;CSS grid&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;a &lt;strong&gt;web font&lt;/strong&gt; (Inter, loaded with &lt;code&gt;@font-face&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;36 line items&lt;/strong&gt;, so the table runs onto page 2 and should repeat its header row there&lt;/li&gt;
&lt;li&gt;totals that shouldn't be split across pages&lt;/li&gt;
&lt;li&gt;US Letter, and a footer with the seller on the left and &lt;strong&gt;"Page 1 of 2"&lt;/strong&gt; on the right&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every library got the same HTML, rendered beforehand from a Razor component; only the page setup (size, margins, page numbers) was added the way each library wants it. The run used .NET 10.0.15 on the &lt;code&gt;ubuntu-24.04&lt;/code&gt; GitHub runner (Ubuntu 24.04.5 LTS).&lt;/p&gt;

&lt;h2&gt;
  
  
  The results
&lt;/h2&gt;

&lt;p&gt;Page 1 from each library:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fc7jjxrwrnnw6esn8aw53.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fc7jjxrwrnnw6esn8aw53.webp" alt="PuppeteerSharp: the invoice exactly as designed, logo and company name side by side, invoice details in two columns, addresses side by side" width="800" height="1035"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;PuppeteerSharp 25.18.1 with Chrome 157: as designed.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdv9g48rm3x5u1c8va56m.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdv9g48rm3x5u1c8va56m.webp" alt="iText pdfHTML: header, web font and table as designed; the invoice details in the top right are stacked in one column" width="800" height="1035"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;iText pdfHTML 6.3.4: almost there. The grid is stacked.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fyy05nm5p3niitqff9ubs.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fyy05nm5p3niitqff9ubs.webp" alt="HtmlRenderer.PdfSharp: page 1 starts directly with the first line item; logo, invoice details, addresses and the table header row are missing" width="800" height="1035"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;HtmlRenderer.PdfSharp 1.6.1: header, addresses, table header and totals are gone.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fng0cez22x0oft08zs3a4.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fng0cez22x0oft08zs3a4.webp" alt="DinkToPdf: logo above the company name, invoice details stacked, bill-to and from stacked instead of side by side" width="800" height="1035"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;DinkToPdf 1.0.8 with wkhtmltopdf 0.12.6.1: Flexbox and grid ignored.&lt;/em&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;PuppeteerSharp&lt;/th&gt;
&lt;th&gt;iText pdfHTML&lt;/th&gt;
&lt;th&gt;HtmlRenderer.PdfSharp&lt;/th&gt;
&lt;th&gt;DinkToPdf&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Engine&lt;/td&gt;
&lt;td&gt;Chrome&lt;/td&gt;
&lt;td&gt;its own&lt;/td&gt;
&lt;td&gt;its own, on PDFsharp&lt;/td&gt;
&lt;td&gt;wkhtmltopdf (Qt WebKit)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Flexbox&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;❌ box not drawn&lt;/td&gt;
&lt;td&gt;❌ ignored&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CSS grid&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;❌ stacked&lt;/td&gt;
&lt;td&gt;❌ box not drawn&lt;/td&gt;
&lt;td&gt;❌ ignored&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Web font&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;❌ fallback font&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Table header on page 2&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;❌ no header row at all&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Page numbers&lt;/td&gt;
&lt;td&gt;footer template&lt;/td&gt;
&lt;td&gt;CSS &lt;code&gt;@page&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;draw them yourself&lt;/td&gt;
&lt;td&gt;footer settings&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Runs JavaScript&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✅ (old WebKit)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;License&lt;/td&gt;
&lt;td&gt;MIT&lt;/td&gt;
&lt;td&gt;AGPL-3.0 or commercial&lt;/td&gt;
&lt;td&gt;BSD-3-Clause&lt;/td&gt;
&lt;td&gt;MIT (wkhtmltopdf: LGPL-3.0)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Latest release&lt;/td&gt;
&lt;td&gt;Nov 2026&lt;/td&gt;
&lt;td&gt;Sep 2026&lt;/td&gt;
&lt;td&gt;Jul 2026&lt;/td&gt;
&lt;td&gt;Apr 2017&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The JavaScript row comes from a second test page with one &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; that changes a word on the page.&lt;/p&gt;

&lt;p&gt;Now the parts that don't fit in a table.&lt;/p&gt;
&lt;h2&gt;
  
  
  PuppeteerSharp: right result, but you're running Chrome
&lt;/h2&gt;

&lt;p&gt;PuppeteerSharp drives a real headless Chrome, so the PDF is what Chrome's print preview shows you. That's the whole appeal, and it delivered.&lt;/p&gt;

&lt;p&gt;The cost shows up in operations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The download.&lt;/strong&gt; &lt;code&gt;BrowserFetcher&lt;/code&gt; pulled Chrome plus its headless shell: &lt;strong&gt;716 MB&lt;/strong&gt; on disk. Put it into your Docker image or onto the server at deploy time, not at the first request.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;"No usable sandbox!"&lt;/strong&gt; Our first CI run died with this. Ubuntu 23.10 and later use AppArmor to restrict the unprivileged user namespaces that Chrome's sandbox needs. The quick fix many answers give is &lt;code&gt;--no-sandbox&lt;/code&gt;. We didn't want that, so the workflow allows the namespaces instead:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;  &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Allow Chrome's sandbox&lt;/span&gt;
    &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;On a disposable CI runner that's fine. On your own servers, an AppArmor profile for the Chrome binary is the narrower fix, because it doesn't lift the restriction for everything else too.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The footer is its own little page.&lt;/strong&gt; &lt;code&gt;FooterTemplate&lt;/code&gt; doesn't see your document's CSS, so it needs inline styles and its own margins. Chrome fills in the page numbers through two class names:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;  &lt;span class="nc"&gt;FooterTemplate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
      &lt;span class="s2"&gt;"""&amp;lt;div style="font-size: 7pt; width: 100%; margin: 0 16mm; display: flex; justify-content: space-between"&amp;gt;"""&lt;/span&gt;
      &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;"""&amp;lt;span&amp;gt;Northwind Outdoor Supply&amp;lt;/span&amp;gt;"""&lt;/span&gt;
      &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;"""&amp;lt;span&amp;gt;Page &amp;lt;span class="pageNumber"&amp;gt;&amp;lt;/span&amp;gt; of &amp;lt;span class="totalPages"&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/div&amp;gt;"""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;And then there's everything that comes with running a browser in production: memory per page, a timeout for pages that never finish loading, and Chrome security updates every few weeks.&lt;/p&gt;
&lt;h2&gt;
  
  
  iText pdfHTML: the best of the browser-free options
&lt;/h2&gt;

&lt;p&gt;iText's HTML add-on got further than I expected: Flexbox, the web font, the repeated table header and CSS paged media all worked. Page numbers go through &lt;code&gt;@page&lt;/code&gt; margin boxes, which is the standard way and very readable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="k"&gt;@page&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Letter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;16mm&lt;/span&gt; &lt;span class="mi"&gt;16mm&lt;/span&gt; &lt;span class="mi"&gt;20mm&lt;/span&gt; &lt;span class="mi"&gt;16mm&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;@bottom-right&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Page "&lt;/span&gt; &lt;span class="nf"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="s2"&gt;" of "&lt;/span&gt; &lt;span class="nf"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pages&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Helvetica&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sans-serif&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;7pt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two things to know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;CSS grid isn't supported.&lt;/strong&gt; The two-column details block came out as one column. If your templates use grid, rewrite those parts with Flexbox or a table.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fonts in margin boxes.&lt;/strong&gt; With &lt;code&gt;font-family: Arial, sans-serif&lt;/code&gt;, the footer came out in Times-Roman. Out of the box iText knows the standard PDF fonts, not the system's Arial. Use Helvetica, or give the converter a font provider with your own fonts.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;No JavaScript, so charts and anything else drawn by a script won't appear. And look at the license before you ship: it's the AGPL-3.0 or a commercial license, and the AGPL has its own rules for software that users reach over a network.&lt;/p&gt;

&lt;h2&gt;
  
  
  HtmlRenderer.PdfSharp: fast, until half the page quietly disappears
&lt;/h2&gt;

&lt;p&gt;This one was the fastest in our run, 0.24 seconds for both pages. Part of the reason: it left out a lot. The logo, the invoice details, both addresses, the table's header row and the totals were simply not there. There was no exception and no warning; the PDF just started with line item 1.&lt;/p&gt;

&lt;p&gt;To find out why, we cut the stylesheet down piece by piece. Remove &lt;code&gt;display: flex&lt;/code&gt; and &lt;code&gt;display: grid&lt;/code&gt;, and the content comes back. HtmlRenderer's own layout engine covers older CSS, and a box it can't lay out isn't drawn at all, its content included. It also didn't use the web font; on the Linux runner it fell back to a font called Tuffy.&lt;/p&gt;

&lt;p&gt;There's no header or footer support either, so page numbers are drawn onto each page with PDFsharp afterwards:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;font&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;XFont&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Arial"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;PageCount&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Pages&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;graphics&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;XGraphics&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FromPdfPage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;XRect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;45&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Height&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Point&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;34&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Width&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Point&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;90&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;graphics&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DrawString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;$&lt;span class="s2"&gt;"Page {i + 1} of {document.PageCount}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;font&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;XBrushes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Black&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;XStringFormats&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CenterRight&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's small, permissively licensed and has no native dependencies. For simple documents built from tables and blocks, that's a good deal. Just render your real templates with it and compare the result with what you expect, because when something is missing, it won't tell you.&lt;/p&gt;

&lt;h2&gt;
  
  
  DinkToPdf: works, but frozen in time
&lt;/h2&gt;

&lt;p&gt;DinkToPdf is a .NET wrapper around wkhtmltopdf's native library. Its last release is from April 2017, and wkhtmltopdf itself has been archived since January 2023. Its Qt WebKit is older than Flexbox and grid as we use them today, so both were ignored and the layout fell back to stacked blocks. The web font, the repeated table header and the page numbers (&lt;code&gt;[page]&lt;/code&gt; and &lt;code&gt;[topage]&lt;/code&gt; in &lt;code&gt;FooterSettings&lt;/code&gt;) worked, and it even runs JavaScript, with an old engine.&lt;/p&gt;

&lt;p&gt;Two practical notes if you maintain an app that uses it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You deploy the &lt;strong&gt;native library&lt;/strong&gt; yourself, for every platform. On Ubuntu 24.04 we installed the 0.12.6.1 package built for 22.04 (jammy), which worked.&lt;/li&gt;
&lt;li&gt;wkhtmltopdf can be &lt;strong&gt;loaded only once per process&lt;/strong&gt;. Create one &lt;code&gt;SynchronizedConverter&lt;/code&gt; for the whole app, as a singleton, not one per request.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For a new project, I wouldn't start here. If you're moving away from it, we wrote up &lt;a href="https://dynamicdocumentapi.com/alternatives/wkhtmltopdf/" rel="noopener noreferrer"&gt;what breaks in wkhtmltopdf and what to replace it with&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  So which one?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;It has to look like it does in Chrome:&lt;/strong&gt; PuppeteerSharp, and plan for running Chrome (image size, sandbox, memory, updates).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No browser, no JavaScript needed, and the license fits:&lt;/strong&gt; iText pdfHTML. Avoid CSS grid.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Simple layouts and a permissive license:&lt;/strong&gt; HtmlRenderer.PdfSharp, after checking that nothing goes missing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;New project:&lt;/strong&gt; not DinkToPdf.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chrome's result without running Chrome:&lt;/strong&gt; that's what a hosted API is for, and it's what we build. The &lt;a href="https://dynamicdocumentapi.com/docs/tutorials/html-to-pdf-csharp" rel="noopener noreferrer"&gt;full tutorial&lt;/a&gt; renders this same invoice through ours with plain &lt;code&gt;HttpClient&lt;/code&gt;: from a template, from a Razor component, and from an ASP.NET Core endpoint with retries. Your HTML and data are sent to us, so check whether that works for your project.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Run it yourself
&lt;/h2&gt;

&lt;p&gt;Everything is in &lt;a href="https://github.com/DynamicDocumentApi/html-to-pdf-csharp" rel="noopener noreferrer"&gt;DynamicDocumentApi/html-to-pdf-csharp&lt;/a&gt;: the invoice, the four renderers in &lt;a href="https://github.com/DynamicDocumentApi/html-to-pdf-csharp/blob/main/compare/Compare/Renderers.cs" rel="noopener noreferrer"&gt;&lt;code&gt;Renderers.cs&lt;/code&gt;&lt;/a&gt;, the &lt;a href="https://github.com/DynamicDocumentApi/html-to-pdf-csharp/blob/main/.github/workflows/compare.yml" rel="noopener noreferrer"&gt;workflow&lt;/a&gt; and the PDFs. The comparison needs no API key:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dotnet run --project compare/Compare -c Release
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you'd configure one of the libraries differently, or know a setting that fixes one of these results, open an issue or a pull request. I'll rerun it and update the results.&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>csharp</category>
      <category>pdf</category>
      <category>webdev</category>
    </item>
    <item>
      <title>ZUGFeRD, Factur-X or XRechnung: Which E-Invoice Format Does Your Customer Need?</title>
      <dc:creator>Frank</dc:creator>
      <pubDate>Thu, 19 Nov 2026 07:02:16 +0000</pubDate>
      <link>https://dev.to/frank_32f82b4e249028949b9/zugferd-factur-x-or-xrechnung-which-e-invoice-format-does-your-customer-need-21l9</link>
      <guid>https://dev.to/frank_32f82b4e249028949b9/zugferd-factur-x-or-xrechnung-which-e-invoice-format-does-your-customer-need-21l9</guid>
      <description>&lt;p&gt;Since 1 January 2025, every business in Germany has to be able to &lt;strong&gt;receive&lt;/strong&gt; e-invoices. From 1 January 2027, the first ones also have to &lt;strong&gt;send&lt;/strong&gt; them, and from 2028 everyone does. If you build or maintain an invoicing feature, the question no longer comes only from accounting but from the product side: which format do we actually send?&lt;/p&gt;

&lt;p&gt;A disclosure up front, because it's relevant: I work on &lt;a href="https://dynamicdocumentapi.com/e-invoicing-api/" rel="noopener noreferrer"&gt;Dynamic Document API&lt;/a&gt;, which among other things generates ZUGFeRD, Factur-X and XRechnung invoices. This article is still meant as a decision guide; the deadlines and formats below come from the official sources (the German Federal Ministry of Finance, FeRD, FNFE-MPE and the EU).&lt;/p&gt;

&lt;h2&gt;
  
  
  What an e-invoice is – and what it isn't
&lt;/h2&gt;

&lt;p&gt;An e-invoice is the invoice as machine-readable XML following the European standard &lt;strong&gt;EN 16931&lt;/strong&gt;. The format is structured so that software can process the invoice without asking questions: line items, tax rates, amounts, all as fields.&lt;/p&gt;

&lt;p&gt;That leads to the most common misconception: a PDF without XML is not an e-invoice, however good it looks. And a PDF with attached XML only counts if the PDF is properly built as PDF/A-3 (more on that below). "E-invoice" is a legal term, not a design choice – and the difference between "we email PDFs" and "we meet the requirement" is exactly that XML file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three names, two forms
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;ZUGFeRD&lt;/strong&gt; is a hybrid: a PDF/A-3 with the XML embedded as an attachment. People read the PDF, software reads the XML. The big practical advantage: nobody has to do without the invoice just because their system can't read structured data yet.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Factur-X&lt;/strong&gt; is technically identical to ZUGFeRD 2.x – the same data container, the same PDF structure, just the French name. If you can do ZUGFeRD, you can do Factur-X. The versions are published together (currently ZUGFeRD 2.5.x / Factur-X 1.09.x).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;XRechnung&lt;/strong&gt; is the opposite: pure XML, no PDF. It is the German profile of EN 16931 with its own business rules (the &lt;code&gt;BR-DE-*&lt;/code&gt; rules), and the reason German public authorities have been receiving structured invoices at all since 2020.&lt;/p&gt;

&lt;p&gt;In the right profiles, all three are EN 16931 compliant. The difference isn't data quality, but who sits at the other end.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which recipient needs what
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Recipient&lt;/th&gt;
&lt;th&gt;Format&lt;/th&gt;
&lt;th&gt;Note&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Public sector in Germany&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;XRechnung&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;The default, usually with a Leitweg-ID (the buyer's routing ID). Federal rules also allow other formats that meet EN 16931, such as ZUGFeRD in the XRECHNUNG profile; the federal states have their own rules. When in doubt, check the tender documents.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Businesses in Germany&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;either&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;ZUGFeRD if someone at the customer still reads the PDF; XRechnung if their system only wants data. When in doubt, ask the customer – that's allowed and saves weeks.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;France&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Factur-X&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;One of the accepted formats. Invoices are exchanged through certified platforms ("plateformes agréées"), not by email.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cross-border B2B within the EU&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;EN 16931&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Mandatory from 1 July 2030 under the EU's ViDA rules.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The practical consequence: there is no single "right" format, only one format per recipient. If you hard-code one into your data model, you're building yourself a migration.&lt;/p&gt;

&lt;h2&gt;
  
  
  The deadlines
&lt;/h2&gt;

&lt;p&gt;Germany:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;1 January 2025&lt;/strong&gt; – receiving is mandatory for all businesses. An email inbox is enough for that, by the way.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;2025 to 2026&lt;/strong&gt; – transition period: everyone may still send paper or PDF invoices (PDF only with the recipient's consent). Businesses with up to €800,000 in prior-year revenue may continue to do so in 2027.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;1 January 2027&lt;/strong&gt; – sending becomes mandatory for businesses with more than €800,000 in prior-year revenue.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;1 January 2028&lt;/strong&gt; – sending becomes mandatory for everyone who invoices other businesses in Germany.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;France:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;1 September 2026&lt;/strong&gt; – receiving is mandatory for all businesses; large and mid-sized businesses must also start sending.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;1 September 2027&lt;/strong&gt; – small and micro businesses follow for sending.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So if you're building an invoicing feature today, you have a year or two, depending on your customers – but not five.&lt;/p&gt;

&lt;h2&gt;
  
  
  Five pitfalls that cost a lot of time
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. MINIMUM and BASIC WL are not e-invoices.&lt;/strong&gt; Both profiles carry only totals, no line items. The German Federal Ministry of Finance explicitly accepts ZUGFeRD from version 2.0.1 &lt;em&gt;except&lt;/em&gt; these two profiles. If you send them, you don't meet the requirement – even if the file says "ZUGFeRD". For B2B, &lt;code&gt;BASIC&lt;/code&gt;, &lt;code&gt;EN 16931&lt;/code&gt; (formerly COMFORT) or &lt;code&gt;EXTENDED&lt;/code&gt; are the safe choices.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. A PDF with attached XML is not a hybrid.&lt;/strong&gt; PDF/A-3 requires, among other things, an output intent with an ICC profile, XMP metadata and a correctly declared relationship for the embedded file (&lt;code&gt;AFRelationship&lt;/code&gt;). Bolting the XML on afterwards shows up in validation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. In hybrid invoices, the XML counts, not the PDF.&lt;/strong&gt; Since 2025, the structured part is authoritative. If the PDF differs, the XML wins – a good reason to generate both parts from the same data source instead of maintaining them in parallel.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. XRechnung has its own rules.&lt;/strong&gt; The &lt;code&gt;BR-DE-*&lt;/code&gt; rules require things nobody misses in B2B: a seller phone number (&lt;code&gt;BR-DE-6&lt;/code&gt;), and for public-sector buyers the Leitweg-ID as the buyer reference. If it's missing, the invoice is invalid – not "almost valid".&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Validate, don't hope.&lt;/strong&gt; For the XML, the KoSIT validator is the reference; for the PDF/A-3, veraPDF. Both are free and fit into a CI pipeline. An invoice that fails at the customer costs more than any test run.&lt;/p&gt;

&lt;p&gt;And the structural trap: if you maintain a separate invoice variant per customer, you'll eventually have five versions of the truth. Better to keep one data model and generate the right output format from it.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to generate it
&lt;/h2&gt;

&lt;p&gt;There are three realistic routes, and all of them are legitimate:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Open source.&lt;/strong&gt; For Java, &lt;a href="https://www.mustangproject.org/" rel="noopener noreferrer"&gt;Mustang&lt;/a&gt; is the usual choice: it writes the CII XML, builds the PDF/A-3 and embeds the file. For Python there's &lt;code&gt;factur-x&lt;/code&gt;, for .NET &lt;code&gt;ZUGFeRD-csharp&lt;/code&gt;. The effort lies less in the XML than in the details – PDF/A conformance, profile rules, a correct tax breakdown.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Commercial libraries&lt;/strong&gt; (iText, PDFlib and others) solve the PDF/A-3 problem cleanly, but cost licence fees and often still need your own work on the data model.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;An API.&lt;/strong&gt; If the invoice comes from a web app anyway, the format part is rarely what you want to build yourself. With Dynamic Document API, you send the invoice data as JSON and get back a ZUGFeRD/Factur-X PDF, validated with KoSIT and veraPDF along the way. Roughly like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"input"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"template"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"template_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"tpl_…"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"data"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"invoice"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"number"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"NW-2026-0042"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"seller"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="err"&gt;…&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"lines"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="err"&gt;…&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"output"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"format"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"pdf"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"einvoice"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"profile"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"EN16931"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"invoice_path"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"invoice"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same &lt;code&gt;data&lt;/code&gt; object, a different &lt;code&gt;profile&lt;/code&gt; value: &lt;code&gt;EN16931&lt;/code&gt; gives you a ZUGFeRD/Factur-X invoice, &lt;code&gt;XRECHNUNG&lt;/code&gt; a PDF that carries an XRechnung. For portals that only accept plain XML, a separate endpoint returns the same invoice as a standalone XRechnung file. The step-by-step tutorial with cURL, Node.js and Python is here: &lt;a href="https://dynamicdocumentapi.com/docs/tutorials/zugferd-xrechnung-invoices-api" rel="noopener noreferrer"&gt;ZUGFeRD, Factur-X and XRechnung invoices with an API&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;For German public authorities: XRechnung. For German businesses: ZUGFeRD if people at the customer still read along, otherwise XRechnung – and when in doubt, ask. For France: Factur-X through a certified platform.&lt;/p&gt;

&lt;p&gt;Technically, the decision is smaller than it looks: all three formats carry the same EN 16931 data. The real work is modelling that data once, completely and correctly – the rest is an output parameter.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;I originally wrote this article in German; the English version was translated with AI assistance.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>api</category>
      <category>tutorial</category>
      <category>fintech</category>
    </item>
    <item>
      <title>Client-Side PDF Tools in JavaScript: Merge, Compress and Convert Without Uploading</title>
      <dc:creator>Frank</dc:creator>
      <pubDate>Wed, 14 Oct 2026 08:05:58 +0000</pubDate>
      <link>https://dev.to/frank_32f82b4e249028949b9/client-side-pdf-tools-in-javascript-merge-compress-and-convert-without-uploading-tags-2d32</link>
      <guid>https://dev.to/frank_32f82b4e249028949b9/client-side-pdf-tools-in-javascript-merge-compress-and-convert-without-uploading-tags-2d32</guid>
      <description>&lt;p&gt;Most "free online PDF tools" work the same way: you upload a contract, a bank statement or a scan of your passport, a server does something to it, and you download the result. That's a lot of trust for merging two files.&lt;/p&gt;

&lt;p&gt;We recently built a set of 18 &lt;a href="https://dynamicdocumentapi.com/tools" rel="noopener noreferrer"&gt;free PDF tools&lt;/a&gt; that do the opposite: the file is read, changed and saved on your device, and its contents never reach a server. The browser can do most of this with two open-source libraries and a &lt;code&gt;&amp;lt;canvas&amp;gt;&lt;/code&gt;. Here's how the pieces fit together, with the code that matters and the limits we ran into.&lt;/p&gt;

&lt;h2&gt;
  
  
  The building blocks
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://github.com/Hopding/pdf-lib" rel="noopener noreferrer"&gt;pdf-lib&lt;/a&gt;&lt;/strong&gt; edits the structure of a PDF: copy pages between documents, rotate them, read and write metadata, draw text and images. It doesn't render anything.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://github.com/mozilla/pdf.js" rel="noopener noreferrer"&gt;PDF.js&lt;/a&gt;&lt;/strong&gt; is Firefox's PDF renderer. It turns pages into pixels and gives you the text layer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The canvas&lt;/strong&gt; decodes and re-encodes images, which is what you need for PDF to image and for compression.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Everything starts the same way: a file input, turned into bytes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'input[type="file"]'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"change"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;files&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;files&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;arrayBuffer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;// files is an array of Uint8Array, nothing has left the device&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And everything ends the same way: bytes, turned back into a download.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;download&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"application/pdf"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createObjectURL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Blob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;link&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assign&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;href&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;download&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;link&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;click&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nb"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;revokeObjectURL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Structural edits with pdf-lib
&lt;/h2&gt;

&lt;p&gt;Merging is the classic example. You create an empty document and copy every page of every source into it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;PDFDocument&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"pdf-lib"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;mergePdfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;files&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nc"&gt;PDFDocument&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;bytes&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="n"&gt;files&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;source&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nc"&gt;PDFDocument&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;updateMetadata&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;pages&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copyPages&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getPageIndices&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;pages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addPage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;useObjectStreams&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Split and extract are the same function with a different list of page indices. Two details are worth copying:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;updateMetadata: false&lt;/code&gt;&lt;/strong&gt; stops pdf-lib from rewriting the producer and dates of the file you just opened. Set them deliberately when you save instead.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Encrypted PDFs throw on load.&lt;/strong&gt; Catch that and say so in plain words, otherwise the user sees a stack trace for a file that opens fine in their viewer.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Rotation has one trap: pages can already carry a rotation, so you add to it instead of overwriting it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;degrees&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"pdf-lib"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;rotatePage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;angle&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getRotation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;angle&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;angle&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;90&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;90&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;360&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;360&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;360&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setRotation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;degrees&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without that, rotating a scanned page that was already stored sideways turns it the wrong way, and you get bug reports you can't reproduce with your own test files.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rendering pages with PDF.js
&lt;/h2&gt;

&lt;p&gt;PDF to PNG or JPEG means rendering each page onto a canvas. PDF.js does the hard part, but its setup decides whether it works under a strict Content Security Policy.&lt;/p&gt;

&lt;p&gt;PDF.js parses documents in a web worker and fetches extra data at runtime: character maps for CJK text, the standard fonts, and in recent versions WebAssembly image decoders and ICC color profiles. We self-host all of them next to the worker, so the CSP can stay at &lt;code&gt;worker-src 'self'&lt;/code&gt; and no third-party origin ever sees a request.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pdfjs&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"pdfjs-dist"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;assets&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sb"&gt;`/vendor/pdfjs/${pdfjs.version}`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;pdfjs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GlobalWorkerOptions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;workerSrc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sb"&gt;`${assets}/pdf.worker.min.mjs`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;openPdf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;pdfjs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getDocument&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// PDF.js transfers the buffer to its worker, so pass a copy and keep the original usable.&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;cMapUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sb"&gt;`${assets}/cmaps/`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;cMapPacked&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;standardFontDataUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sb"&gt;`${assets}/standard_fonts/`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;promise&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;bytes.slice()&lt;/code&gt; matters. Without it, the original array is detached after the first call, and any second operation on the same file fails with an empty buffer.&lt;/p&gt;

&lt;p&gt;Rendering itself is a matter of scale. PDF coordinates are points, 72 per inch, so 150 dpi means a scale of 150 / 72:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;MAX_PIXELS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;16_000_000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Safari refuses canvases much larger than this&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;pageToImage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pdf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pageNumber&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;dpi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;150&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"image/png"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;pdf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getPage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pageNumber&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;unit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getViewport&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;scale&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dpi&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;72&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;pixels&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;unit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;unit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;height&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;scale&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pixels&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;MAX_PIXELS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;scale&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;MAX_PIXELS&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;pixels&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;viewport&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getViewport&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;scale&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;canvas&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"canvas"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;viewport&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;height&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;viewport&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;viewport&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="c1"&gt;// JPEG has no transparency: paint white behind pages without a background.&lt;/span&gt;
    &lt;span class="n"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"image/jpeg"&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="s2"&gt;"#ffffff"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;promise&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;blob&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toBlob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.9&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cleanup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;height&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// release the canvas memory right away&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The pixel cap matters more than it looks. A large-format drawing at 300 dpi easily exceeds what Safari will allocate, and the render fails or comes back blank instead of throwing a useful error. Capping the area and scaling down keeps the output useful on every browser.&lt;/p&gt;

&lt;h2&gt;
  
  
  Compression without a server
&lt;/h2&gt;

&lt;p&gt;Compression sounds like it needs a server. It doesn't, as long as you only touch images. Most of the weight in a typical oversized PDF is photos and scans, so &lt;a href="https://dynamicdocumentapi.com/tools/compress-pdf" rel="noopener noreferrer"&gt;our compressor&lt;/a&gt; leaves text, vector graphics and fonts alone and only touches embedded images:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Walk the document's objects with pdf-lib and collect the image streams. Images under 256 pixels on their longest side aren't worth the effort and stay as they are.&lt;/li&gt;
&lt;li&gt;Decode them. JPEGs go straight to the canvas. Flate-compressed raw images are inflated with &lt;a href="https://github.com/101arrowz/fflate" rel="noopener noreferrer"&gt;fflate&lt;/a&gt;, and PNG predictors are undone by hand.&lt;/li&gt;
&lt;li&gt;Draw the pixels onto a canvas, scaled so the longest side stays below a limit, and export a JPEG at lower quality.&lt;/li&gt;
&lt;li&gt;Keep the new version only if it's at least 10% smaller. Otherwise the original stays. Then set the stream's filter and dimensions and save with object streams.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We offer three levels. The strongest keeps the longest image side at 1,200 pixels and JPEG quality 0.6, which is roughly 150 dpi on a full A4 page. The lightest keeps 2,600 pixels at 0.85.&lt;/p&gt;

&lt;p&gt;Some images are better left alone than rewritten badly: CMYK, indexed colors and 16-bit samples would change color on the way through a canvas, so we skip them. Images with a soft mask, the PDF way of storing transparency, get recompressed but keep their pixel size, so image and mask still line up. At the end the tool reports how many images it found and how many it actually recompressed. An honest "we saved 4%" beats a smaller file that looks wrong.&lt;/p&gt;

&lt;h2&gt;
  
  
  HTML to PDF: the one thing a browser can only print
&lt;/h2&gt;

&lt;p&gt;A browser has exactly one way to turn HTML into a PDF: print it. So the HTML to PDF tool renders your markup in a sandboxed iframe and opens the print dialog, where you pick "Save as PDF". Page size and margins come from CSS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="k"&gt;@page&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;A4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;15mm&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;@media&lt;/span&gt; &lt;span class="nt"&gt;print&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nc"&gt;.no-print&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Chrome and Edge the result comes from Chromium's layout engine, so it's faithful to modern CSS. What you can't do from a web page is skip the dialog, control the browser's own headers and footers reliably, or wait for remote fonts and scripts the way a headless browser can. That's the point where client-side stops and you need &lt;a href="https://dynamicdocumentapi.com/url-to-pdf-api/" rel="noopener noreferrer"&gt;Chromium running on a server&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Write it once, test it in Node
&lt;/h2&gt;

&lt;p&gt;One design choice saved us a lot of time: every operation is a plain function from bytes to bytes, and anything that needs a canvas gets one passed in.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;RasterEnvironment&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Canvas&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Output&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;createCanvas&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Canvas&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Canvas&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"png"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"jpeg"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;quality&lt;/span&gt;&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Output&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the browser, the environment creates DOM canvases and calls &lt;code&gt;toBlob&lt;/code&gt;. In the test suite, the same functions run in Node with a native canvas package. Merge, split, rotate, compress and PDF to image are all covered by unit tests, without spinning up a browser for every case.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where the browser runs out
&lt;/h2&gt;

&lt;p&gt;Client-side isn't free of trade-offs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Memory is the user's.&lt;/strong&gt; Files of several hundred megabytes depend on how much memory the tab gets. When a file fails to open, closing other tabs often helps.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rendering is PDF.js, not Acrobat.&lt;/strong&gt; Unusual font encodings and some blend modes can look slightly different from desktop viewers. XFA forms we turn off entirely.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Text you add needs fonts.&lt;/strong&gt; pdf-lib's built-in standard fonts only cover Latin characters. Watermarks or page numbers in other scripts need an embedded font, and failing with a clear message beats drawing empty boxes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No automation.&lt;/strong&gt; A tool in a tab is great for one file and useless for ten thousand a night. That's what a &lt;a href="https://dynamicdocumentapi.com/document-generation-api/" rel="noopener noreferrer"&gt;server-side API&lt;/a&gt; is for.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Read files with &lt;code&gt;arrayBuffer()&lt;/code&gt;, hand back results with a Blob URL, and nothing ever needs to be uploaded.&lt;/li&gt;
&lt;li&gt;pdf-lib covers structure, PDF.js covers pixels and text, and the canvas covers image encoding.&lt;/li&gt;
&lt;li&gt;Self-host the PDF.js worker and its assets, pass it a copy of your bytes, and cap canvas sizes.&lt;/li&gt;
&lt;li&gt;Keep operations as pure byte-in, byte-out functions so they run in Node for tests.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you've built something similar, which PDF edge cases bit you?&lt;/p&gt;




&lt;p&gt;&lt;em&gt;I originally wrote this article in German; the English version was translated with AI assistance.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>privacy</category>
      <category>webdev</category>
      <category>pdf</category>
    </item>
  </channel>
</rss>
