<?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: Rohit Tilante</title>
    <description>The latest articles on DEV Community by Rohit Tilante (@rohittilante).</description>
    <link>https://dev.to/rohittilante</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%2F4249715%2Ff38489ff-4c69-419b-a26a-8af3ca812fbe.png</url>
      <title>DEV Community: Rohit Tilante</title>
      <link>https://dev.to/rohittilante</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rohittilante"/>
    <language>en</language>
    <item>
      <title>Hitting an exact KB range in the browser: how we compress exam photos and PDFs without a server</title>
      <dc:creator>Rohit Tilante</dc:creator>
      <pubDate>Tue, 17 Nov 2026 14:28:36 +0000</pubDate>
      <link>https://dev.to/rohittilante/hitting-an-exact-kb-range-in-the-browser-how-we-compress-exam-photos-and-pdfs-without-a-server-44ke</link>
      <guid>https://dev.to/rohittilante/hitting-an-exact-kb-range-in-the-browser-how-we-compress-exam-photos-and-pdfs-without-a-server-44ke</guid>
      <description>&lt;p&gt;Indian exam and job portals are strict about file sizes in a way most compressors aren't built for. SBI wants a photo of &lt;strong&gt;20–50 KB&lt;/strong&gt;. UPSC wants a signature image of &lt;strong&gt;20–100 KB&lt;/strong&gt; at 350–500 px. SSC wants a signature of &lt;strong&gt;10–20 KB&lt;/strong&gt;. Upload 51 KB and you're rejected. Upload 19 KB and you're rejected too.&lt;/p&gt;

&lt;p&gt;We built &lt;a href="https://toolfreely.com/?utm_source=devto&amp;amp;utm_medium=article" rel="noopener noreferrer"&gt;ToolFreely&lt;/a&gt; to hit those windows, entirely in the browser, because the files are faces, signatures and marksheets that nobody should have to upload to a stranger's server. &lt;em&gt;(Disclosure: I built it.)&lt;/em&gt; Here's how it works.&lt;/p&gt;

&lt;h3&gt;
  
  
  The stack, briefly
&lt;/h3&gt;

&lt;p&gt;The site is static Astro, and each tool page hydrates a React island on load. Everything heavy is loaded only when needed: pdf.js and pdf-lib download when you open a PDF, and the HEIC decoder when you pick a HEIC file. A visitor who only resizes a JPEG never downloads about a megabyte of PDF code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem 1: "KB" has two meanings
&lt;/h3&gt;

&lt;p&gt;Some portals count 1 KB as 1,000 bytes, others as 1,024. We can't know which, so we target the &lt;strong&gt;intersection&lt;/strong&gt;:&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="c1"&gt;// simplified from the production bundle&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;maxBytes&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;maxKb&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;48&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// strict ceiling, small safety margin&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;minBytes&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;ceil&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;minKb&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        &lt;span class="c1"&gt;// strict floor&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A file between those two numbers passes under either convention. The UI also prints the result three ways, e.g. &lt;code&gt;32,827 bytes · 32.8 KB (1,000) · 32.1 KB (1,024)&lt;/code&gt;, so users can check it against whatever the portal says.&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem 2: Hitting a window, not just a ceiling
&lt;/h3&gt;

&lt;p&gt;Most "compress to X KB" tools lower quality until the file is under X. With a 20–50 KB window, that often lands on 12 KB or misses entirely. We do a &lt;strong&gt;binary search on JPEG quality&lt;/strong&gt;, aiming at the middle of 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;let&lt;/span&gt; &lt;span class="n"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.08&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.92&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                 &lt;span class="c1"&gt;// quality bounds&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="n"&gt;step&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;step&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;hi&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mf"&gt;0.03&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;step&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="n"&gt;q&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&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="n"&gt;bytes&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;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;q&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="c1"&gt;// canvas.toBlob('image/jpeg', q)&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;bytes&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;win&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;maxBytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;hi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;else&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;bytes&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;win&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;minBytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&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;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;win&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;maxBytes&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mf"&gt;0.04&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// close enough&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;hi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&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;If the file is still too big at the lowest quality, we shrink the dimensions instead of destroying the image. The new scale is estimated from how far over we are, &lt;code&gt;scale * Math.sqrt(maxBytes / bytes) * 0.92&lt;/code&gt;, with a floor of 15% of the original size. Then the search runs again. There's a hard cap of 12 encodes in total, so a phone never gets stuck. The result reports whether it landed &lt;code&gt;inside&lt;/code&gt;, &lt;code&gt;under&lt;/code&gt; or &lt;code&gt;over&lt;/code&gt;, and the UI tells the user which.&lt;/p&gt;

&lt;p&gt;Presets where the notice demands &lt;em&gt;exact&lt;/em&gt; pixels, or centimetres at a stated DPI, turn off down-scaling, so only quality moves. A "preferred" size like SBI's 200 × 230 px may still shrink if that's the only way to fit.&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem 3: Minimums are as real as maximums
&lt;/h3&gt;

&lt;p&gt;Plenty of people have the opposite problem: "your file must be at least 10 KB". A tiny, clean signature can compress to 6 KB. Re-encoding at higher quality doesn't always help.&lt;/p&gt;

&lt;p&gt;Our "increase size" mode pads the JPEG instead. It inserts a &lt;strong&gt;COM (comment) segment&lt;/strong&gt; (&lt;code&gt;0xFF 0xFE&lt;/code&gt;) just before the end-of-image marker. The pixels don't change, decoders ignore the comment, and the byte count goes up to the minimum.&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem 4: Photos from phones lie about their orientation
&lt;/h3&gt;

&lt;p&gt;Phone photos often store the rotation in EXIF rather than in the pixels. We decode with &lt;code&gt;createImageBitmap(blob, { imageOrientation: 'none' })&lt;/code&gt;, read the EXIF orientation ourselves, and apply the matching canvas transform (all 8 cases). That way every browser gives the same result, and a cropped passport photo never comes out sideways.&lt;/p&gt;

&lt;p&gt;Some presets give sizes in centimetres plus a DPI. For those we also write the density into the JPEG's JFIF header, or the &lt;code&gt;pHYs&lt;/code&gt; chunk for PNG with its CRC, so "3.5 × 4.5 cm at 200 DPI" means the same thing in an image viewer.&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem 5: PDFs are pictures more often than you think
&lt;/h3&gt;

&lt;p&gt;Most "PDF too big" files are scans: each page is a photo. Our PDF compressor is deliberately simple:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Render each page with pdf.js to a canvas.&lt;/li&gt;
&lt;li&gt;Optionally convert to grayscale (BT.601 luma: &lt;code&gt;0.299R + 0.587G + 0.114B&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Encode each page as JPEG and add up the bytes.&lt;/li&gt;
&lt;li&gt;Try a fixed ladder of render scale × quality, &lt;code&gt;[1.5, 1.15, 0.85] × [0.72, 0.55, 0.42]&lt;/code&gt;, and stop at the first combination that fits.&lt;/li&gt;
&lt;li&gt;Rebuild the document with pdf-lib by embedding each page JPEG.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The important design decision is what happens when nothing fits. The tool &lt;strong&gt;throws an error instead of writing a smaller, unreadable file&lt;/strong&gt;: "Going further would make the text hard to read, so no file was saved." A blurry marksheet that gets rejected after the deadline is worse than an honest error now.&lt;/p&gt;

&lt;p&gt;The trade-off is stated in the UI too: pages become images, so the text is no longer selectable. There are also hard limits of &lt;strong&gt;30 MB and 20 pages&lt;/strong&gt;, because every page is rendered to a canvas on the user's own device. Twenty pages is the cap that keeps a mid-range phone from running out of memory while it renders them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem 6: Specs are data, not copy
&lt;/h3&gt;

&lt;p&gt;The exam limits live in a JSON array. Each preset has &lt;code&gt;min_kb&lt;/code&gt;, &lt;code&gt;max_kb&lt;/code&gt;, pixel or centimetre dimensions, DPI, notes, and a &lt;code&gt;source_url&lt;/code&gt; pointing to the official notice PDF. The UI shows a "last checked" date. When a notice says "about 6.0 cm × 2.0 cm" but gives no DPI, the tool stops and says: "This notice gives centimetres but no DPI. Choose 200 or 300 — that choice is not written in the notice." When a new notice comes out, the preset is updated to match it.&lt;/p&gt;

&lt;h3&gt;
  
  
  How we check "no upload"
&lt;/h3&gt;

&lt;p&gt;A Content-Security-Policy isn't proof: ours still allows &lt;code&gt;connect-src https:&lt;/code&gt; for analytics and ads. So we test it the boring way. We load the page in a headless browser, run the tool, and log every request. After a file is picked, the only outgoing call is a small first-party analytics beacon to &lt;code&gt;/a/e&lt;/code&gt;. It carries the event name, the page path and the screen width; pageviews also include the referrer and UTM source. The file never leaves the tab. &lt;em&gt;(The pages do show Google AdSense ads, which is how the site stays free.)&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What we'd tell anyone building this
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Design for windows, not ceilings.&lt;/strong&gt; A minimum KB is a common failure.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pick the strictest reading of ambiguous units,&lt;/strong&gt; and show the user the raw bytes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lazy-load the heavy stuff.&lt;/strong&gt; Most visitors need only one tool.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fail loudly&lt;/strong&gt; rather than hand back a file that will be rejected.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Verify privacy claims with a network log,&lt;/strong&gt; not with marketing copy.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you want to poke at it, the &lt;a href="https://toolfreely.com/compress-image-to-50kb/?utm_source=devto&amp;amp;utm_medium=article" rel="noopener noreferrer"&gt;20–50 KB photo compressor&lt;/a&gt; and the &lt;a href="https://toolfreely.com/compress-pdf-to-500kb/?utm_source=devto&amp;amp;utm_medium=article" rel="noopener noreferrer"&gt;PDF-to-500 KB compressor&lt;/a&gt; are the two pages described here. Open DevTools → Network while you use them. Weird PDFs and edge cases are welcome in the comments.&lt;/p&gt;

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