<?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: Sad1247</title>
    <description>The latest articles on DEV Community by Sad1247 (@sad1247).</description>
    <link>https://dev.to/sad1247</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%2F4220946%2F7d4c234f-3cee-4800-a3fc-49b317c1febe.png</url>
      <title>DEV Community: Sad1247</title>
      <link>https://dev.to/sad1247</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sad1247"/>
    <language>en</language>
    <item>
      <title>I run 38 PDF tools with no server. Here is where the work went instead.</title>
      <dc:creator>Sad1247</dc:creator>
      <pubDate>Wed, 11 Nov 2026 14:46:32 +0000</pubDate>
      <link>https://dev.to/sad1247/i-run-38-pdf-tools-with-no-server-here-is-where-the-work-went-instead-18m8</link>
      <guid>https://dev.to/sad1247/i-run-38-pdf-tools-with-no-server-here-is-where-the-work-went-instead-18m8</guid>
      <description>&lt;p&gt;When you merge two PDFs on most websites, your files go to someone's server, get processed there, and come back. I built &lt;a href="https://pdfroll.com/en/" rel="noopener noreferrer"&gt;PDFRoll&lt;/a&gt; the other way around: 38 tools, static pages, and nothing that processes a file anywhere but in your own browser tab.&lt;/p&gt;

&lt;p&gt;That sounds like a privacy feature, and it is. But it is mostly an architecture decision, and it moved the hard problems to places I did not expect. This post is about those places.&lt;/p&gt;

&lt;h2&gt;
  
  
  What "no server" means here
&lt;/h2&gt;

&lt;p&gt;The site is static HTML on a CDN. The PDF work is done by libraries shipped to the browser:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;pdf-lib&lt;/strong&gt; to create and rewrite PDFs&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PDF.js&lt;/strong&gt; to read and render them&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;qpdf compiled to WebAssembly&lt;/strong&gt; for encryption and password removal&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tesseract&lt;/strong&gt; (WebAssembly again) for OCR&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;libheif&lt;/strong&gt; for iPhone photos, plus a few converters for Word, Excel and PowerPoint files&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The only thing a visitor's browser ever sends to my side is a counter. More on that below, because what it is allowed to count turned out to matter.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem 1: you can't ship all of that up front
&lt;/h2&gt;

&lt;p&gt;Those libraries add up to megabytes. If a page loaded them all, the person who came to rotate one page would pay for an OCR engine.&lt;/p&gt;

&lt;p&gt;So nothing heavy loads with the page. The core trio (pdf-lib, PDF.js, a zip library) loads on the visitor's &lt;strong&gt;first gesture&lt;/strong&gt;: the first pointer press, key press, or file dragged over the window.&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;preloadCore&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;events&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'pointerdown'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'touchstart'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'keydown'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'dragenter'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'focusin'&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;go&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;events&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;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;removeEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;go&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="nf"&gt;loadCore&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;catch&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="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="n"&gt;events&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;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;go&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;capture&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;passive&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;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The page stays light for crawlers and for people who only read. By the time someone has picked a file in the dialog, the libraries are there. Everything else (OCR, qpdf, the Office converters) loads only when its tool actually runs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem 2: "compress" is two different jobs
&lt;/h2&gt;

&lt;p&gt;On a server you would hand the file to Ghostscript and be done. In a browser there is no Ghostscript, so I had to decide what compression even means.&lt;/p&gt;

&lt;p&gt;There are two honest options:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Lossless rewrite.&lt;/strong&gt; Copy the pages into a fresh document, drop unused objects, pack the rest into compressed object streams. Text stays text. The gain is often small.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rasterize.&lt;/strong&gt; Render each page to a canvas, encode it as JPEG, and build a new PDF from the images. The gain is large, and the text is no longer selectable.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Neither one is right every time, so the tool runs both and compares:&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;raster&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;out&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="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// At similar weight, prefer lossless: it keeps the text.&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;lossless&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;lossless&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;raster&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;1.15&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="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;lossless&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'lossless'&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="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;raster&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'raster'&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;That 15% margin is the whole policy. If keeping real text costs at most 15% more bytes, keep the text.&lt;/p&gt;

&lt;p&gt;A side effect I like: for files up to 30 pages, the tool computes all three compression levels as soon as the file is chosen and shows the resulting size next to each option. The results are cached, so clicking "Compress" after that is instant. A server would charge me for those speculative runs. Here they cost the visitor a few seconds of idle CPU.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem 3: the limit moved to the visitor's device
&lt;/h2&gt;

&lt;p&gt;This is the real price of the design. My costs are close to zero, so there is no file size cap and no daily quota. But the ceiling did not disappear. It moved to the RAM of whoever is using the site.&lt;/p&gt;

&lt;p&gt;Rendering a page to a canvas takes uncompressed pixels. A long scanned document on a five-year-old phone can run out of memory, and the browser does not fail politely. You get a &lt;code&gt;RangeError&lt;/code&gt;, or "array buffer allocation failed", or sometimes a call stack overflow from deep inside a library.&lt;/p&gt;

&lt;p&gt;So every tool runs through one error classifier, and memory failures get their own message:&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;MEMORY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;out&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="n"&gt;memory&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;allocation&lt;/span&gt; &lt;span class="n"&gt;failed&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="n"&gt;allocation&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;invalid&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;maximum&lt;/span&gt; &lt;span class="n"&gt;call&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;i&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="no"&gt;MEMORY&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nc"&gt;RangeError&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="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'memoire'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'This file is too heavy for this device\'s memory. Try a smaller file, or a computer.'&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;It is not a fix. It is telling the person the truth about why it failed and what to do next, which is the best I can do when the machine is not mine.&lt;/p&gt;

&lt;p&gt;Speed has the same shape. In my first week of measurements, 4 of the 10 compressions that finished took more than 30 seconds. That is a tiny sample and some of those runs were my own tests, but it is enough to know where the next work is: moving the rendering loop into a worker.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem 4: measuring without seeing anything
&lt;/h2&gt;

&lt;p&gt;With a server, you learn what breaks by reading logs. I have no logs, and the entire promise of the site is that I never see the files.&lt;/p&gt;

&lt;p&gt;But I still needed to know whether people who start a tool actually finish. So one small endpoint accepts a tiny JSON event: which tool, whether it started, finished or failed, a duration bucket, the page language, and the referring hostname. No cookie, no visitor ID, counters aggregated per day.&lt;/p&gt;

&lt;p&gt;The interesting constraint came with failures. An error message can contain a file name. So the raw message never leaves the browser. The classifier above maps every failure to one code from a fixed list (memory, password-protected, damaged file, and a few more), and the server keeps a whitelist:&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;CAUSES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'memoire'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'protege'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'abime'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'outil'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'autre'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="cm"&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;cause&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;CAUSES&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Anything that is not on the list is dropped. The same function that picks the message shown to the visitor also picks the code that gets counted, so the two can't drift apart.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I would tell someone starting this
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Static plus WebAssembly goes much further than I assumed.&lt;/strong&gt; Encryption, OCR and Office conversion all run fine in a tab.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Your scaling problem becomes a support problem.&lt;/strong&gt; You will never get paged at night. You will get "it froze on my phone", and you can't reproduce it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Decide early what you refuse to know.&lt;/strong&gt; It is much easier to design measurement around "the file name never leaves the page" on day one than to retrofit it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you want to check the claim rather than take my word for it, open the network tab on the &lt;a href="https://pdfroll.com/en/compress-pdf/" rel="noopener noreferrer"&gt;compressor&lt;/a&gt; and run a file through it. You will see the libraries arrive, and then no request that carries your file: only a small counter and the page analytics beacon.&lt;/p&gt;

&lt;p&gt;I'm curious how others handle the memory ceiling. If you have shipped heavy canvas work to low-end phones, what actually helped?&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>webassembly</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Hitting an exact file size when compressing images in the browser</title>
      <dc:creator>Sad1247</dc:creator>
      <pubDate>Sun, 08 Nov 2026 23:04:14 +0000</pubDate>
      <link>https://dev.to/sad1247/hitting-an-exact-file-size-when-compressing-images-in-the-browser-1lil</link>
      <guid>https://dev.to/sad1247/hitting-an-exact-file-size-when-compressing-images-in-the-browser-1lil</guid>
      <description>&lt;p&gt;India's chartered accountancy board caps the photo you upload with your exam form at 50 KB. South Africa's tax service wants supporting documents under 2 MB. A French rental platform stops at 10 MB per file. Canada's immigration portal has its own number.&lt;/p&gt;

&lt;p&gt;None of these portals will compress your file for you. They reject it and let you work it out.&lt;/p&gt;

&lt;p&gt;A photo from a modern phone is 3 to 5 MB. Getting it to 50 KB means dividing its weight by about eighty, and the obvious approach — pick a JPEG quality, encode, hope — doesn't survive contact with real files. Here's what actually works, and why the naive version fails.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quality is not a dial you can read
&lt;/h2&gt;

&lt;p&gt;The temptation is to map file size onto quality: 50 KB is small, so use quality 0.3.&lt;/p&gt;

&lt;p&gt;That fails because JPEG quality isn't a size setting. It's a quantization parameter, and how many bytes it produces depends entirely on the image. A photo of a blank wall at quality 0.9 can be smaller than a photo of a forest at quality 0.4. Noise, texture and detail decide the output size, not the number you passed in.&lt;/p&gt;

&lt;p&gt;So you can't compute the quality you need. You have to find it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Binary search
&lt;/h2&gt;

&lt;p&gt;Encoding to a canvas is cheap enough to do repeatedly, which makes this a search problem. The quality range you care about is roughly 0.25 to 0.95 — below 0.25 the artifacts are bad enough that the result is useless anyway.&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;let&lt;/span&gt; &lt;span class="nx"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;hi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.95&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;found&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&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;let&lt;/span&gt; &lt;span class="nx"&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="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&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;const&lt;/span&gt; &lt;span class="nx"&gt;q&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;hi&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&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="nf"&gt;encodeImage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bmp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;q&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;type&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="nx"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;found&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;q&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nx"&gt;hi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;q&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;Seven iterations narrows a 0.7-wide range to about 0.005. That's far finer than the eye can tell apart, and it costs seven canvas encodes, which on a phone photo runs in well under a second.&lt;/p&gt;

&lt;p&gt;Note what the loop keeps. Every time it lands under the target it stores that blob in &lt;code&gt;found&lt;/code&gt; and searches &lt;em&gt;upward&lt;/em&gt;, because a bigger file that still fits is a better file. It never returns the first result that happens to work; it returns the best one it found.&lt;/p&gt;

&lt;p&gt;The helper doing the actual encoding:&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;canvasToBlob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;quality&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="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&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="nx"&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="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;quality&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&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;encodeImage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bmp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;quality&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;type&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="nx"&gt;c&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;canvas&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&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;max&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="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="nx"&gt;bmp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt;  &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&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;max&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="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="nx"&gt;bmp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;height&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ctx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;2d&lt;/span&gt;&lt;span class="dl"&gt;'&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="nx"&gt;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;image/jpeg&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fillStyle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#fff&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fillRect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;height&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;imageSmoothingQuality&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;high&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;drawImage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bmp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;height&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&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="nf"&gt;canvasToBlob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;quality&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&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;That white rectangle is not decoration. Canvas starts transparent, and transparent pixels become &lt;strong&gt;black&lt;/strong&gt; when you encode to JPEG, which has no alpha channel. Drop a PNG with a transparent background into a naive compressor and you get your subject on a black square. Filling white first costs one operation and removes an entire category of bug report.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;c.width = c.height = 0&lt;/code&gt; at the end matters too. You're allocating a full-resolution canvas seven times in a row; on a 48-megapixel phone photo that's a lot of memory held by an element nothing references yet. Zeroing the dimensions releases the backing buffer immediately instead of waiting for the garbage collector to notice.&lt;/p&gt;

&lt;h2&gt;
  
  
  When quality alone isn't enough
&lt;/h2&gt;

&lt;p&gt;Binary search solves the easy case. It doesn't solve 4 MB to 50 KB, because even quality 0.25 at full resolution won't get there. At some point you have to make the image physically smaller.&lt;/p&gt;

&lt;p&gt;The order matters, and it's worth being deliberate about it. Reducing JPEG quality degrades a photograph more gracefully than shrinking it does — you lose fine detail and gain some mushiness, but the picture stays legible. Shrinking throws away pixels permanently. So you exhaust quality at each resolution before giving up resolution:&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;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;imageToTarget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bmp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;progress&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="nx"&gt;best&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&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="nx"&gt;scale&lt;/span&gt; &lt;span class="k"&gt;of&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="mf"&gt;0.85&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.55&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.45&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.35&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.18&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.12&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;progress&lt;/span&gt;&lt;span class="p"&gt;?.(&lt;/span&gt;&lt;span class="s2"&gt;`Trying &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="nx"&gt;bmp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt; px…`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;hi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.95&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;found&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&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;let&lt;/span&gt; &lt;span class="nx"&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="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&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;const&lt;/span&gt; &lt;span class="nx"&gt;q&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;hi&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&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="nf"&gt;encodeImage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bmp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;q&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;type&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="nx"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;found&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;q&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nx"&gt;hi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;q&lt;/span&gt;&lt;span class="p"&gt;;&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="nx"&gt;found&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;found&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;reached&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;low&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;encodeImage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bmp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;type&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="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;best&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;low&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;best&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;best&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;low&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="nx"&gt;low&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;low&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;reached&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="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;best&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;reached&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two details in there are worth pulling out.&lt;/p&gt;

&lt;p&gt;The extra &lt;code&gt;low&lt;/code&gt; encode after a failed search looks redundant — the binary search already tried qualities near 0.25. It didn't, quite. The search probes the &lt;em&gt;midpoints&lt;/em&gt; of a shrinking interval, so when every probe comes back too large, the lowest quality it actually tried is somewhere above 0.25. That one extra encode at the floor sometimes succeeds where the search didn't, and it saves a whole scale step.&lt;/p&gt;

&lt;p&gt;The other is &lt;code&gt;reached&lt;/code&gt;. The function returns the smallest thing it managed to produce even when it fails, along with a flag saying it failed. That lets the UI say "we got it to 68 KB, which is as small as this image goes" instead of throwing. A user who's been told the honest number can crop the photo and try again. A user who got an exception has no idea what to do.&lt;/p&gt;

&lt;p&gt;Worst case is nine scales × eight encodes, but in practice it exits in the first or second scale, because most files aren't trying to shrink by a factor of eighty.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why do this client-side at all
&lt;/h2&gt;

&lt;p&gt;The images going through this are passport photos, signatures, bank statements and immigration paperwork. The whole category of "resize your exam photo" sites is uploading those to a server, and almost none of them say what happens to the file afterwards.&lt;/p&gt;

&lt;p&gt;Canvas encoding runs entirely in the page. Nothing is sent anywhere, which you can confirm in the network tab in about five seconds. For this particular class of file, that's not a nice-to-have.&lt;/p&gt;

&lt;p&gt;The practical cost is that you're limited to what the browser can encode — JPEG, PNG, WebP — and &lt;code&gt;canvas.toBlob&lt;/code&gt; quality is only honoured for lossy formats. PNG ignores the quality argument entirely, which is why the code converts to JPEG when it's chasing a size target.&lt;/p&gt;

&lt;h2&gt;
  
  
  The limits, honestly
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Decoding is the memory ceiling, not encoding.&lt;/strong&gt; &lt;code&gt;createImageBitmap&lt;/code&gt; on a 48-megapixel photo allocates around 190 MB of uncompressed pixels before you've encoded anything. On older phones that's where it falls over.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;HEIC needs help.&lt;/strong&gt; Safari decodes it natively; nothing else does. Everywhere else you need a decoder library before you have a bitmap to work with.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cropping beats compressing.&lt;/strong&gt; This is the thing worth telling users, because it's counterintuitive and it dominates everything the algorithm does. Half the pixels at the same file size means twice the detail per pixel. A tightly cropped passport photo at 50 KB looks fine; a full-body shot squeezed into the same 50 KB looks like a JPEG from 1998. No amount of search strategy fixes framing.&lt;/p&gt;




&lt;p&gt;The code above is what runs in &lt;a href="https://pdfroll.com/en/compress-image/" rel="noopener noreferrer"&gt;PDFRoll's image compressor&lt;/a&gt; — free, no account, nothing uploaded. If you want to see the target-size path in action, the &lt;a href="https://pdfroll.com/en/compress-photo-for-icai-exam/" rel="noopener noreferrer"&gt;50 KB ICAI exam photo page&lt;/a&gt; preloads the constraint.&lt;/p&gt;

&lt;p&gt;If you've found a better strategy than quality-then-scale, I'd like to hear it. I suspect there's a smarter first guess than 0.6 available from the image's entropy, but I haven't found one that beats just running the search.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>canvas</category>
      <category>performance</category>
    </item>
    <item>
      <title>Government upload limits are a mess, so I documented all 18 of them</title>
      <dc:creator>Sad1247</dc:creator>
      <pubDate>Sun, 01 Nov 2026 17:18:31 +0000</pubDate>
      <link>https://dev.to/sad1247/government-upload-limits-are-a-mess-so-i-documented-all-18-of-them-16e</link>
      <guid>https://dev.to/sad1247/government-upload-limits-are-a-mess-so-i-documented-all-18-of-them-16e</guid>
      <description>&lt;p&gt;A few months ago someone told me their permanent residence application had been returned because a scanned document was too big. They had compressed it to 4 MB, which is what the article they read said. The portal they were using allows 5 MB — and the file they actually needed to shrink was a different one.&lt;/p&gt;

&lt;p&gt;That sent me down a hole. I went looking for a table of official upload limits and could not find one that was both correct and sourced. What I found instead was a dozen blog posts confidently quoting numbers that contradict each other, usually with no link to anything official and no date.&lt;/p&gt;

&lt;p&gt;So I built the table. Eighteen limits, each one linked to the page that states it, each one dated. Here is what I learned building it.&lt;/p&gt;

&lt;h2&gt;
  
  
  A single "service" often has several different limits
&lt;/h2&gt;

&lt;p&gt;This is the part that burns people, and almost nothing on the web mentions it.&lt;/p&gt;

&lt;p&gt;Canada's immigration department (IRCC) does not have one upload limit. The secure account, permanent residence and citizenship streams cap each file at 4 MB. The IRCC Portal allows 5 MB. Same department, same applicant, different ceiling depending on which door you came in through. If you compress to 5 MB because that is the number you read, and you are in the secure account, your upload fails.&lt;/p&gt;

&lt;p&gt;France's DossierFacile does the same thing in a different direction: 10 MB per file, but 50 MB per &lt;em&gt;document&lt;/em&gt;, where a document can be several pages uploaded together. Whether you scan your payslips one by one or as a single block decides which limit applies to you.&lt;/p&gt;

&lt;p&gt;Any table that lists one number per organisation is wrong for at least some of its readers. Mine has five rows just for IRCC.&lt;/p&gt;

&lt;h2&gt;
  
  
  The range is five orders of magnitude
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Service&lt;/th&gt;
&lt;th&gt;Limit&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;ICAI exam photo (India)&lt;/td&gt;
&lt;td&gt;50 KB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;French state services upload&lt;/td&gt;
&lt;td&gt;1 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IRCC secure account (Canada)&lt;/td&gt;
&lt;td&gt;4 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SARS eFiling (South Africa)&lt;/td&gt;
&lt;td&gt;5 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Legal Aid Agency CCMS (UK)&lt;/td&gt;
&lt;td&gt;8 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Microsoft 365 / Exchange&lt;/td&gt;
&lt;td&gt;10 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;DossierFacile per file (France)&lt;/td&gt;
&lt;td&gt;10 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Gmail&lt;/td&gt;
&lt;td&gt;25 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SEI (Brazil)&lt;/td&gt;
&lt;td&gt;50 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CRA document submission (Canada)&lt;/td&gt;
&lt;td&gt;150 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Slack&lt;/td&gt;
&lt;td&gt;1 GB&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The 50 KB at the top is not a typo. Indian professional exam portals cap the candidate photo and signature at 50 KB each, and every exam season generates a wave of people trying to get a phone photo under that. A modern phone photo is 3 to 5 MB. You need roughly a 100× reduction, which no "compress my image" slider is going to give you without thinking about dimensions first.&lt;/p&gt;

&lt;h2&gt;
  
  
  Limits move, and nobody announces it
&lt;/h2&gt;

&lt;p&gt;The French state upload fiche I used as a source is from 2018. It is still the most specific public document I could find, and it is almost certainly stale. I flagged it in the table rather than quietly presenting it as current, which is the whole reason the table has a &lt;code&gt;checked&lt;/code&gt; date on it: &lt;code&gt;2026-10-17&lt;/code&gt; at the time of writing.&lt;/p&gt;

&lt;p&gt;That date is the most useful column. A number without a date is a rumour.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why the compression has to happen client-side
&lt;/h2&gt;

&lt;p&gt;Here is the part that bothers me about how this problem is usually solved.&lt;/p&gt;

&lt;p&gt;The documents people need to shrink to meet these limits are, overwhelmingly: passports, bank statements, payslips, medical forms, tax filings, proof of funds. The standard advice is "use a free online PDF compressor," which means uploading exactly those documents to a server belonging to a company you have never heard of, under a privacy policy you did not read, in order to satisfy a government portal's file size rule.&lt;/p&gt;

&lt;p&gt;That is a strange trade to make, and it is entirely avoidable. Everything needed to compress, merge, split, convert and OCR a PDF runs fine in a browser now:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;pdf-lib&lt;/strong&gt; for structural work — merge, split, rotate, page numbering, metadata&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PDF.js&lt;/strong&gt; for rendering pages to canvas, which is what image-based compression comes down to&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tesseract compiled to WebAssembly&lt;/strong&gt; for OCR, which is the one I expected to be unusable in a browser and which turned out to be fine&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Canvas + &lt;code&gt;toBlob&lt;/code&gt;&lt;/strong&gt; with a quality parameter for the actual size reduction&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The performance is acceptable. A 40-page scanned PDF compresses in a few seconds on a mid-range laptop, which is slower than a server would be, and nobody cares, because the alternative was uploading their passport.&lt;/p&gt;

&lt;p&gt;The architectural consequence is the interesting bit: there is no upload endpoint in the application at all. Not a locked-down one, not one that deletes files after an hour — none. That is a claim you can verify from the network tab rather than from a privacy policy, which is the only kind of privacy claim worth making.&lt;/p&gt;

&lt;h2&gt;
  
  
  The table
&lt;/h2&gt;

&lt;p&gt;It lives here, with the official source linked on every row and the date each one was last checked:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://pdfroll.com/en/pdf-size-limits/" rel="noopener noreferrer"&gt;Official upload size limits for government portals and services&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The tools that go with it are at &lt;a href="https://pdfroll.com" rel="noopener noreferrer"&gt;pdfroll.com&lt;/a&gt; — free, no account, and everything runs locally.&lt;/p&gt;

&lt;p&gt;If you know of a limit I am missing, or one that has moved since I checked it, tell me in the comments and I will add it with the source. That is the only way a table like this stays true.&lt;/p&gt;

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