<?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: Usama Muhammad</title>
    <description>The latest articles on DEV Community by Usama Muhammad (@usama_muhammad_d144933dd0).</description>
    <link>https://dev.to/usama_muhammad_d144933dd0</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%2F4225213%2F9bd5a83d-3131-40e6-8d59-5033325cbb89.png</url>
      <title>DEV Community: Usama Muhammad</title>
      <link>https://dev.to/usama_muhammad_d144933dd0</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/usama_muhammad_d144933dd0"/>
    <language>en</language>
    <item>
      <title>Sending an invoice PDF to WhatsApp from a web app (Web Share API, html2canvas, jsPDF)</title>
      <dc:creator>Usama Muhammad</dc:creator>
      <pubDate>Sat, 21 Nov 2026 11:45:31 +0000</pubDate>
      <link>https://dev.to/usama_muhammad_d144933dd0/sending-an-invoice-pdf-to-whatsapp-from-a-web-app-web-share-api-html2canvas-jspdf-1g6</link>
      <guid>https://dev.to/usama_muhammad_d144933dd0/sending-an-invoice-pdf-to-whatsapp-from-a-web-app-web-share-api-html2canvas-jspdf-1g6</guid>
      <description>&lt;p&gt;Most of the small businesses using our invoicing app, &lt;a href="https://tichkuly.com" rel="noopener noreferrer"&gt;Tichkuly&lt;/a&gt;, don't email their customers. They WhatsApp them. So the one button that really matters in the app is "Send on WhatsApp", and it has to send a real PDF file, not just a link.&lt;/p&gt;

&lt;p&gt;The app is a Next.js web app (wrapped for Android too), so everything below happens in the browser. Here's how it works and what tripped us up.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Render the invoice to a PDF in the browser
&lt;/h2&gt;

&lt;p&gt;The invoice is already on screen as HTML, laid out as an A4 page (794 × 1123 CSS pixels, which is A4 at 96 dpi). We take a picture of that element with &lt;code&gt;html2canvas&lt;/code&gt; and place it on an A4 page with &lt;code&gt;jsPDF&lt;/code&gt;:&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="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;renderPdf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;HTMLElement&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&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;File&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Both libraries are big, so they load only when someone actually makes a PDF.&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;html2canvas&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;jsPDF&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;]&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="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"html2canvas-pro"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"jspdf"&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="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="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;html2canvas&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;element&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;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;useCORS&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;backgroundColor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"#ffffff"&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;pdf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;jsPDF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;unit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"pt"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;format&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"a4"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;compress&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="kd"&gt;const&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;pdf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;internal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pageSize&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getWidth&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;imageHeight&lt;/span&gt; &lt;span class="o"&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="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;)&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;width&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="nf"&gt;addImage&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="nf"&gt;toDataURL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"image/jpeg"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"JPEG"&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="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;imageHeight&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="s2"&gt;"FAST"&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;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;pdf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;output&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"blob"&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;filename&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="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="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 worth copying:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;html2canvas-pro&lt;/code&gt; instead of &lt;code&gt;html2canvas&lt;/code&gt;.&lt;/strong&gt; Tailwind v4 uses &lt;code&gt;oklch()&lt;/code&gt; colours, and the original library throws on them. The &lt;code&gt;-pro&lt;/code&gt; fork handles them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;JPEG at quality 0.5.&lt;/strong&gt; With &lt;code&gt;scale: 2&lt;/code&gt;, text stays sharp, and a page is about a third of the size it is at 0.92. That matters when the file goes over a mobile connection to WhatsApp.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The trade-off: the text in the PDF is an image, so it can't be selected or searched. For a one-page invoice sent to a customer's phone, we found that acceptable. If you need selectable text, build the PDF with jsPDF's text API or &lt;code&gt;pdf-lib&lt;/code&gt; instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Hand the file to WhatsApp with the Web Share API
&lt;/h2&gt;

&lt;p&gt;On phones, &lt;code&gt;navigator.share()&lt;/code&gt; can share files, and the share sheet lists WhatsApp, WhatsApp Business, Gmail and the rest:&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="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;sharePdf&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="nc"&gt;File&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&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="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;canShare&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="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="n"&gt;file&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="nf"&gt;downloadPdf&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="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// most desktops: save it, attach by hand&lt;/span&gt;
    &lt;span class="k"&gt;return&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="k"&gt;try&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;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;share&lt;/span&gt;&lt;span class="p"&gt;(&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="n"&gt;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;title&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="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&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;catch&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="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// The user closing the share sheet is not an error.&lt;/span&gt;
    &lt;span class="k"&gt;if&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="k"&gt;as&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="kt"&gt;string&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;name&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s2"&gt;"AbortError"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="n"&gt;e&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="kc"&gt;true&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;Always check &lt;code&gt;navigator.canShare({ files })&lt;/code&gt; first. Plenty of browsers have &lt;code&gt;navigator.share&lt;/code&gt; but can't share files, and desktop Chrome on most systems is one of them.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. The gotcha: make the PDF &lt;em&gt;before&lt;/em&gt; the tap
&lt;/h2&gt;

&lt;p&gt;This is the one that cost us a day. &lt;code&gt;navigator.share()&lt;/code&gt; must be called during a &lt;strong&gt;user activation&lt;/strong&gt;, right after a tap. Rendering the PDF takes long enough (half a second to a couple of seconds on a cheap Android phone) that by the time it's ready, the activation has expired. Then &lt;code&gt;share()&lt;/code&gt; fails with a &lt;code&gt;NotAllowedError&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The fix: make the PDF quietly in the background as soon as the invoice is on screen and saved, and keep it. The tap only shares a file that already exists.&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="c1"&gt;// A moment after the page settles, make the PDF so the first tap can share at once.&lt;/span&gt;
&lt;span class="nf"&gt;useEffect&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="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="n"&gt;enabled&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="c1"&gt;// e.g. unsaved changes&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;timer&lt;/span&gt; &lt;span class="o"&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="nf"&gt;make&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="mi"&gt;600&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="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;clearTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timer&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;enabled&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;version&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;&lt;code&gt;version&lt;/code&gt; is a string that changes whenever the invoice content changes, so an edited invoice never sends a stale PDF. &lt;code&gt;make()&lt;/code&gt; returns the cached file if the version matches.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. A link as well as the file
&lt;/h2&gt;

&lt;p&gt;Some people prefer a WhatsApp message with a link they can open later. So after the PDF is made, we also upload a copy under the document's private share token, and the message carries &lt;code&gt;https://.../i/&amp;lt;token&amp;gt;&lt;/code&gt;. The upload runs in the background and never blocks the share. We also track which version is online, so an older upload that finishes late doesn't overwrite a newer one.&lt;/p&gt;

&lt;h2&gt;
  
  
  What we'd do differently
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;If we needed text you can select or search, we'd build the PDF from data with &lt;code&gt;pdf-lib&lt;/code&gt; rather than taking a picture of HTML.&lt;/li&gt;
&lt;li&gt;Multi-page documents need care. We lay long ones out as separate A4 page elements and render each one, so a table row is never cut across two pages.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can try the in-browser version without signing up in our &lt;a href="https://tichkuly.com/free-invoice-generator" rel="noopener noreferrer"&gt;free invoice generator&lt;/a&gt;. It makes the PDF on your device, and nothing is uploaded. If you just want blank Word or Excel templates, they're free on &lt;a href="https://github.com/ub3248/free-invoice-templates" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Questions about any of this are welcome in the comments.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>pdf</category>
      <category>pwa</category>
    </item>
    <item>
      <title>One colour behind the pointer: what a TV remote taught us about focus highlights</title>
      <dc:creator>Usama Muhammad</dc:creator>
      <pubDate>Sat, 21 Nov 2026 11:39:58 +0000</pubDate>
      <link>https://dev.to/usama_muhammad_d144933dd0/one-colour-behind-the-pointer-what-a-tv-remote-taught-us-about-focus-highlights-2i7o</link>
      <guid>https://dev.to/usama_muhammad_d144933dd0/one-colour-behind-the-pointer-what-a-tv-remote-taught-us-about-focus-highlights-2i7o</guid>
      <description>&lt;p&gt;On a phone you tap what you want. On a television you push arrows on a remote, and the only thing telling you where you are is the focus highlight. In the media player I build (NewTV Player, a React Native app that also runs on Android TV, Fire TV and smart TVs) the highlight was corrected more times than any other part of the app. This post is what we ended up with, and why.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: this post was written with help from an AI assistant, from our own project notes and measurements.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The rule
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;One colour behind the pointer, in any position, on any screen.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That sounds obvious. It broke in three different ways, and all three shipped at some point.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Ring plus fill
&lt;/h3&gt;

&lt;p&gt;A bright accent border drawn around a see-through accent wash is two colours on one row. On a real screenshot we measured the ring at &lt;code&gt;rgb(246,197,67)&lt;/code&gt; around a fill of &lt;code&gt;rgb(101,87,39)&lt;/code&gt;. From the sofa it looks like a box inside a box.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Split rows
&lt;/h3&gt;

&lt;p&gt;A row made of several focus targets (a channel name plus a favourite star, say), where each target paints its own background, shows the focused part in the accent colour and its neighbours in the tile colour. Two or three tones across one row.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. A competing "active" state
&lt;/h3&gt;

&lt;p&gt;The selected tab or the chosen season painted with a second fill, inside the same bar the highlight moves through, adds a third tone. The eye can't tell which one is "here".&lt;/p&gt;

&lt;h2&gt;
  
  
  What fixed it
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The fill is the whole highlight.&lt;/strong&gt; When a focusable view changes its own background, it draws no ring at all.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The fill is solid, not see-through.&lt;/strong&gt; We blend the accent into the tile colour at 30% and paint that. A translucent wash took on whatever was underneath, so the same highlight read brown on the dark header and yellow on a lighter card.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No &lt;code&gt;elevation&lt;/code&gt; on a focused target.&lt;/strong&gt; On Android the shadow is drawn under the view and showed through the fill as a second, darker band inside the highlight. We only caught this on a photo of a real TV.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A focused target hides its own border&lt;/strong&gt; (&lt;code&gt;borderColor: 'transparent'&lt;/code&gt;), so an outlined button doesn't keep its outline around the fill.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chosen items use coloured text or a coloured icon, never a fill of their own.&lt;/strong&gt; Tabs, search chips, seasons and guide categories all follow this.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;One ground for multi-target rows.&lt;/strong&gt; A small &lt;code&gt;FocusRow&lt;/code&gt; wrapper paints the tile colour once for the whole row; the children are transparent, and only the focused child paints the highlight. Moving onto a channel colours the channel and leaves the star alone.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Text that sits on the highlight
&lt;/h2&gt;

&lt;p&gt;A warning in red under a playlist name ("Subscription ended") measured &lt;strong&gt;2.29 to 1&lt;/strong&gt; contrast on the gold fill, on the very row the pointer starts on. Instead of putting a box behind it, the text changes itself while its row has focus: the red is moved toward the normal text colour just far enough to reach 4.5 to 1. We measured 4.50 to 4.74 across all four themes and five accent colours. Off the pointer it is the plain red again.&lt;/p&gt;

&lt;h2&gt;
  
  
  A React Native trap: &lt;code&gt;TouchableOpacity&lt;/code&gt; eats focus
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;TouchableOpacity&lt;/code&gt; strips &lt;code&gt;onFocus&lt;/code&gt; and &lt;code&gt;onBlur&lt;/code&gt; before they reach the underlying view, on purpose, to keep old behaviour. A focus handler on it is never called, so the highlight never appears. Use &lt;code&gt;Pressable&lt;/code&gt;, which passes both through. This one cost us a long evening.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to settle arguments about it
&lt;/h2&gt;

&lt;p&gt;Take a screenshot, then sample pixels across the focused row at several heights, in the gaps between the text. A correct row gives back &lt;strong&gt;one distinct colour&lt;/strong&gt;. A search for the ring colour gives back nothing. It turns "does this look right?" into a test that passes or fails.&lt;/p&gt;




&lt;p&gt;If you're building for TV with React Native, I hope this saves you a few rounds. The app is at &lt;a href="https://newtvplayer.com" rel="noopener noreferrer"&gt;newtvplayer.com&lt;/a&gt;; it's a player for your own playlist and carries no content itself. Questions about the focus code are welcome in the comments.&lt;/p&gt;

</description>
      <category>reactnative</category>
      <category>android</category>
      <category>a11y</category>
      <category>ux</category>
    </item>
    <item>
      <title>Loading a 72 MB JSON list on a TV box without running out of memory</title>
      <dc:creator>Usama Muhammad</dc:creator>
      <pubDate>Sat, 14 Nov 2026 09:52:38 +0000</pubDate>
      <link>https://dev.to/usama_muhammad_d144933dd0/loading-a-72-mb-json-list-on-a-tv-box-without-running-out-of-memory-n33</link>
      <guid>https://dev.to/usama_muhammad_d144933dd0/loading-a-72-mb-json-list-on-a-tv-box-without-running-out-of-memory-n33</guid>
      <description>&lt;p&gt;I make a media player for Android TV boxes, phones and Windows. People sign in with the playlist their provider gives them, and the app downloads three lists from the provider's server: live channels, films and series. Each one is a single JSON array.&lt;/p&gt;

&lt;p&gt;On paper that is easy: &lt;code&gt;await (await fetch(url)).json()&lt;/code&gt;. In practice it broke on real hardware, and the fix taught me more about memory on cheap devices than anything else this year.&lt;/p&gt;

&lt;h2&gt;
  
  
  How big these lists really are
&lt;/h2&gt;

&lt;p&gt;These are measured numbers from real accounts, not guesses:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;one list alone: &lt;strong&gt;72 MB&lt;/strong&gt; of JSON&lt;/li&gt;
&lt;li&gt;the three lists of one account together: &lt;strong&gt;156 MB&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;another account: 28,445 channels, 157,163 films and 50,149 series, 131 MB in total&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Reading one of those whole needs several times its size in memory at the same moment: the raw bytes, the string made from them, and every object &lt;code&gt;JSON.parse&lt;/code&gt; builds. On an Android TV box with 4 GB of RAM the download simply failed. The fetch threw "Network request failed", our app showed "No internet connection", and the playlist stayed empty. The user's internet was fine. The box had run out of room.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rule one: never hold the list
&lt;/h2&gt;

&lt;p&gt;The fix was to stop treating the response as one value.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Stream the body.&lt;/strong&gt; The list is read as the bytes arrive, a piece at a time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cut the array, don't parse it.&lt;/strong&gt; A small splitter walks only the outline of the array: brackets, braces and strings. When it has one complete item it hands that item's text to &lt;code&gt;JSON.parse&lt;/code&gt; on its own. A 72 MB list becomes tens of thousands of tiny parses.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Back-pressure.&lt;/strong&gt; The splitter hands items over 2,000 at a time and reads no further until the database has taken them. Memory stays flat no matter how long the list is.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Save in fixed blocks.&lt;/strong&gt; Rows go into SQLite through one prepared &lt;code&gt;INSERT&lt;/code&gt; of 2,900 rows that is built once and reused. Leftover rows wait for the next batch, so every statement is the same shape.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  The trap: backslashes
&lt;/h2&gt;

&lt;p&gt;The first version of the splitter was fast on one server and painfully slow on another, with lists of the same size.&lt;/p&gt;

&lt;p&gt;PHP writes every &lt;code&gt;/&lt;/code&gt; as &lt;code&gt;\/&lt;/code&gt;, so lists from PHP panels are full of escapes. The splitter looked for the next backslash inside each string to know whether a quote was escaped. On a list written as plain JSON there are no backslashes at all, so every search ran to the end of the piece. In Node, 150,000 films (50 MB) took 0.15 s in PHP's format and 6 s as plain JSON, and minutes when the pieces were 1 MB.&lt;/p&gt;

&lt;p&gt;The fix: remember where the next backslash is and search again only after you pass it. Same for the next quote. Now both formats take about 0.1 s, and the time grows only with the size of the list, not with what is inside it.&lt;/p&gt;

&lt;p&gt;A related case: PHP writes every non-ASCII letter as &lt;code&gt;\uXXXX&lt;/code&gt;. 20,000 test series with Arabic plots (72 MB) took 3.6 s before that change and 0.5 s after.&lt;/p&gt;

&lt;h2&gt;
  
  
  On Android: a file beats the network stream
&lt;/h2&gt;

&lt;p&gt;Streaming fixed the crashes, but the box was still slow. The timers showed the cost was turning many small network pieces (8 KB each) into text on the JavaScript thread.&lt;/p&gt;

&lt;p&gt;So on Android the list now goes to a file in the cache folder first, written by native code, and is read back &lt;strong&gt;one megabyte at a time&lt;/strong&gt;. A megabyte is in memory, never the list. Text that arrives in large runs is also text the engine parses quickly: &lt;code&gt;JSON.parse&lt;/code&gt; of a 78 MB film list took 0.55 s in megabyte runs, against 4 s when the same runs were put together from 8 KB pieces.&lt;/p&gt;

&lt;p&gt;On one list of 200,000 films that meant about 4 s to read back, parse, build rows and save, where the same work took about 12 s before. If the device has less than 300 MB free, or the file cannot be written, the app falls back to reading the list as it arrives.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I would tell anyone loading big JSON on a TV box
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Measure on the device. My PC was never the problem.&lt;/li&gt;
&lt;li&gt;Treat "Network request failed" with suspicion on low-memory hardware. It may be memory, not network.&lt;/li&gt;
&lt;li&gt;Never &lt;code&gt;response.json()&lt;/code&gt; something whose size you don't control.&lt;/li&gt;
&lt;li&gt;Keep batches a fixed size so statements can be prepared once.&lt;/li&gt;
&lt;li&gt;Test with the ugliest real data you can get: escaped slashes, &lt;code&gt;\u&lt;/code&gt; escapes, huge single items.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you want to see the result, the app is &lt;a href="https://newtvplayer.com" rel="noopener noreferrer"&gt;NewTV Player&lt;/a&gt;. It is a player only, with no content of its own: you add the playlist from your provider. I'm its developer, so take the plug for what it is; the techniques above work in any React Native app.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This post was drafted by an AI assistant from my own project notes and measurements.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>reactnative</category>
      <category>android</category>
      <category>performance</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Reading a 150 MB playlist on a 4 GB TV box without running out of memory</title>
      <dc:creator>Usama Muhammad</dc:creator>
      <pubDate>Wed, 04 Nov 2026 02:08:56 +0000</pubDate>
      <link>https://dev.to/usama_muhammad_d144933dd0/reading-a-150-mb-playlist-on-a-4-gb-tv-box-without-running-out-of-memory-3gcg</link>
      <guid>https://dev.to/usama_muhammad_d144933dd0/reading-a-150-mb-playlist-on-a-4-gb-tv-box-without-running-out-of-memory-3gcg</guid>
      <description>&lt;p&gt;I build NewTV Player, a media player for people's own playlists (an M3U link or an Xtream login from their provider). It runs on phones, Android TV boxes, Fire TV and Windows, and it is written in React Native with Expo and SQLite.&lt;/p&gt;

&lt;p&gt;The hardest bug I have fixed in it had nothing to do with video. It was a JSON file.&lt;/p&gt;

&lt;h2&gt;
  
  
  The bug: an empty playlist on a 4 GB box
&lt;/h2&gt;

&lt;p&gt;A user on an Android TV box with 4 GB of memory added their subscription. The progress bar moved, the app worked for a while, and then the playlist was simply empty. No crash report, no error on screen.&lt;/p&gt;

&lt;p&gt;The reason was size. Xtream panels answer three calls for a catalogue: live channels, films and series. Each one is a single JSON array. For real subscriptions we measured &lt;strong&gt;72 MB in one list and 156 MB across the three&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The first version did what most code does:&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;const&lt;/span&gt; &lt;span class="n"&gt;res&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;fetch&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="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;text&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;That line holds the response body as a string, then builds a JavaScript object for every item, with all its fields, while the string is still alive. On a 72 MB list that costs several times the file size in memory. A phone with 8 GB shrugs. A TV box with 4 GB, sharing it with the launcher, the video decoder and everything else, kills the app quietly, and the playlist stays empty.&lt;/p&gt;

&lt;p&gt;Plain M3U files are worse. The largest one we have seen measured &lt;strong&gt;340 MB with 1,234,673 entries&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The rule we wrote down
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Never read a provider's list whole.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Everything below follows from that one sentence.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: put the list on disk first
&lt;/h2&gt;

&lt;p&gt;On Android the response goes straight to a file, never into a JavaScript string. The download writes bytes to storage as they arrive, so memory use during the download is a small buffer, whatever the list size.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: read it back a megabyte at a time
&lt;/h2&gt;

&lt;p&gt;The file is then read back one megabyte at a time. A megabyte is in memory, never the list.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: a streaming parser that hands over small batches
&lt;/h2&gt;

&lt;p&gt;A JSON array of objects can be split without a full parser: track string state (inside quotes or not, escaped or not) and brace depth, and every time depth returns to the array level you have one complete object. You can &lt;code&gt;JSON.parse&lt;/code&gt; that one small object safely.&lt;/p&gt;

&lt;p&gt;The reader collects about &lt;strong&gt;2,000 items&lt;/strong&gt; and hands them over as a batch. The caller never sees more than that at once. M3U files get the same treatment with a line reader: one &lt;code&gt;#EXTINF&lt;/code&gt; line plus its URL is one entry.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: write to SQLite in big statements, not one row at a time
&lt;/h2&gt;

&lt;p&gt;Inserting 245,000 films one &lt;code&gt;INSERT&lt;/code&gt; at a time is slow, because each statement has a fixed cost. Packing many rows into one statement is far faster, but SQLite limits how many bound values one statement may carry. We save &lt;strong&gt;2,900 rows a statement&lt;/strong&gt;, which keeps us under that limit with our column count.&lt;/p&gt;

&lt;p&gt;One trap here: the insert helper takes a fixed column count. If you add a column, every row builder that calls it has to change too. We have five (live, films, series, M3U, and a demo playlist), and missing one makes the inserts fail silently.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: stage the update, then swap
&lt;/h2&gt;

&lt;p&gt;An update writes into staging tables while the old catalogue stays on screen. Only when everything has arrived does it swap. If the connection drops halfway, the viewer still has yesterday's playlist instead of half of today's.&lt;/p&gt;

&lt;h2&gt;
  
  
  Results
&lt;/h2&gt;

&lt;p&gt;On a real box, a subscription with about 16,000 channels and 245,000 films now loads in roughly two minutes, without the system killing the app on the way. The 4 GB box that showed an empty playlist now shows all of it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I would tell anyone parsing big responses on TV hardware
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Measure the real response sizes from real users. Ours were far bigger than we had guessed.&lt;/li&gt;
&lt;li&gt;Treat &lt;code&gt;JSON.parse(await res.text())&lt;/code&gt; as a red flag for anything that can grow.&lt;/li&gt;
&lt;li&gt;Disk is cheap on these boxes; memory is not. Spill to a file first.&lt;/li&gt;
&lt;li&gt;Batch everything: parsing, inserts, UI updates.&lt;/li&gt;
&lt;li&gt;Test on the weakest box you can buy, not on your phone.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you want to try it, NewTV Player is on Google Play and the Microsoft Store: &lt;a href="https://newtvplayer.com" rel="noopener noreferrer"&gt;https://newtvplayer.com&lt;/a&gt;. I'm happy to answer questions about any of this in the comments.&lt;/p&gt;

</description>
      <category>android</category>
      <category>reactnative</category>
      <category>performance</category>
      <category>sqlite</category>
    </item>
  </channel>
</rss>
