<?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: OneFileKit</title>
    <description>The latest articles on DEV Community by OneFileKit (@onefilekit).</description>
    <link>https://dev.to/onefilekit</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%2F4224836%2F7a24d239-cf86-4bc0-ab49-f486913614b4.png</url>
      <title>DEV Community: OneFileKit</title>
      <link>https://dev.to/onefilekit</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/onefilekit"/>
    <language>en</language>
    <item>
      <title>JPG and PNG to PDF Without Re-encoding: img2pdf vs Pillow</title>
      <dc:creator>OneFileKit</dc:creator>
      <pubDate>Tue, 24 Nov 2026 06:00:42 +0000</pubDate>
      <link>https://dev.to/onefilekit/jpg-and-png-to-pdf-without-re-encoding-img2pdf-vs-pillow-4gi0</link>
      <guid>https://dev.to/onefilekit/jpg-and-png-to-pdf-without-re-encoding-img2pdf-vs-pillow-4gi0</guid>
      <description>&lt;p&gt;I often need to turn a handful of scans or screenshots into a single PDF. The job sounds trivial, and in Python it's tempting to open the images with Pillow and call &lt;code&gt;.save("out.pdf")&lt;/code&gt;. That works, and it also quietly re-encodes your pictures. For a photograph you may not notice. For a screenshot or a signed form, you will. Here's the difference between wrapping the original bytes and converting them, and the tool I actually use.&lt;/p&gt;

&lt;h2&gt;
  
  
  What "to PDF" should mean
&lt;/h2&gt;

&lt;p&gt;A PDF page can hold a JPEG almost as-is: the same compressed bytes, with a small amount of PDF wrapping around them. If that's what you do, the image in the PDF is the image you started with. No new compression artefacts, no change in sharpness, and the file grows only by the size of the container.&lt;/p&gt;

&lt;p&gt;The other approach is to decode the image and encode it again, often as a new JPEG, when the PDF is written. That's a conversion, not a wrap. You pay for it in quality, and sometimes in file size too.&lt;/p&gt;

&lt;h2&gt;
  
  
  img2pdf: wrap, don't convert
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://pypi.org/project/img2pdf/" rel="noopener noreferrer"&gt;img2pdf&lt;/a&gt; is a small tool with one job: put raster images into a PDF without re-encoding PNG, JPEG or JPEG 2000. Other formats are stored with the same kind of lossless compression PNG uses.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip install img2pdf
&lt;span class="c"&gt;# Debian/Ubuntu also has a package: sudo apt install img2pdf&lt;/span&gt;

img2pdf photo.jpg receipt.png -o album.pdf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each image becomes a page, in the order you list them. The same thing from Python:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;img2pdf&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;pathlib&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Path&lt;/span&gt;

&lt;span class="nc"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"album.pdf"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write_bytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;img2pdf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;convert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"photo.jpg"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"receipt.png"&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;I tested this with a 36,875-byte JPEG. The PDF came out at 38,324 bytes — a little over a kilobyte of container, and that's it. &lt;code&gt;pdfimages -list&lt;/code&gt; (from poppler-utils) reported the image as &lt;code&gt;jpeg&lt;/code&gt;. Extracting it with &lt;code&gt;pdfimages -j&lt;/code&gt; gave me a file with the same MD5 hash as the original photograph. The JPEG inside the PDF &lt;em&gt;was&lt;/em&gt; the JPEG I started with.&lt;/p&gt;

&lt;p&gt;For a sharp PNG (a fake invoice with hard-edged text), img2pdf stored the image losslessly (&lt;code&gt;enc&lt;/code&gt; showed as &lt;code&gt;image&lt;/code&gt; in &lt;code&gt;pdfimages -list&lt;/code&gt;), and the PDF was only slightly larger than the PNG.&lt;/p&gt;

&lt;p&gt;By default, if the file has no DPI of its own, img2pdf assumes 96 dpi and makes the page the same size as the image. There's no white border. When you need a standard paper size for printing or for a form, set it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;img2pdf --pagesize A4 --fit into photo.jpg -o a4.pdf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;--fit into&lt;/code&gt; scales the picture so it sits entirely on the page. &lt;code&gt;letter&lt;/code&gt; works as well as &lt;code&gt;A4&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Pillow actually writes
&lt;/h2&gt;

&lt;p&gt;Pillow can write a PDF directly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="no"&gt;PIL&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Image&lt;/span&gt;

&lt;span class="nc"&gt;Image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"photo.jpg"&lt;/span&gt;&lt;span class="p"&gt;)&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="s2"&gt;"out.pdf"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"PDF"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Several pages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="no"&gt;PIL&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Image&lt;/span&gt;

&lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"photo.jpg"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;rest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;Image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"receipt.png"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;convert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"RGB"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;first&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="s2"&gt;"out.pdf"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"PDF"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;save_all&lt;/span&gt;&lt;span class="o"&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;append_images&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;rest&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;convert("RGB")&lt;/code&gt; is needed for a PNG that still has an alpha channel; PDF pages as Pillow writes them are RGB.&lt;/p&gt;

&lt;p&gt;I saved the same JPEG through Pillow. &lt;code&gt;pdfimages -list&lt;/code&gt; still said &lt;code&gt;jpeg&lt;/code&gt;, but the extracted image was &lt;em&gt;not&lt;/em&gt; the original: different hash, different size, reported at 72 dpi rather than 96. Pillow had decoded it and written a new JPEG.&lt;/p&gt;

&lt;p&gt;The PNG result was more surprising. img2pdf had stored it losslessly at 10.2K. Pillow wrote a JPEG, and the image inside the PDF was 18.7K — larger &lt;em&gt;and&lt;/em&gt; lossy. Fine edges in screenshots and text are exactly what JPEG is bad at, so this is the case I care about most.&lt;/p&gt;

&lt;p&gt;Pillow is the right tool when you're already in a Pillow pipeline and you need to compose, resize or watermark before you write the PDF. It is not the right tool if your only goal is "these files, as they are, in a PDF".&lt;/p&gt;

&lt;h2&gt;
  
  
  A quick comparison
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;img2pdf&lt;/th&gt;
&lt;th&gt;Pillow &lt;code&gt;Image.save(..., "PDF")&lt;/code&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;JPEG&lt;/td&gt;
&lt;td&gt;Original bytes, wrapped&lt;/td&gt;
&lt;td&gt;Re-encoded as a new JPEG&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PNG&lt;/td&gt;
&lt;td&gt;Lossless&lt;/td&gt;
&lt;td&gt;Re-encoded as JPEG&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Default page size&lt;/td&gt;
&lt;td&gt;Image size at 96 dpi (if no DPI is set)&lt;/td&gt;
&lt;td&gt;Image size at 72 dpi&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Multi-page&lt;/td&gt;
&lt;td&gt;List the files, in order&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;save_all=True&lt;/code&gt; and &lt;code&gt;append_images&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If you want to confirm what a tool did, &lt;code&gt;pdfimages -list output.pdf&lt;/code&gt; is the first thing I run. Look at the &lt;code&gt;enc&lt;/code&gt; column (&lt;code&gt;jpeg&lt;/code&gt; vs &lt;code&gt;image&lt;/code&gt;) and, if it matters, extract the image and compare it to the original.&lt;/p&gt;

&lt;h2&gt;
  
  
  When I don't write a script
&lt;/h2&gt;

&lt;p&gt;For a folder of scans I want to repeat next month, I use img2pdf. When someone just needs a couple of photos turned into a PDF, I point them at a web tool rather than talking them through pip. I built free &lt;a href="https://onefilekit.com/jpg-to-pdf" rel="noopener noreferrer"&gt;JPG to PDF&lt;/a&gt; and &lt;a href="https://onefilekit.com/png-to-pdf" rel="noopener noreferrer"&gt;PNG to PDF&lt;/a&gt; converters on OneFileKit, with no sign-up. You upload the images, put them in order, and download a single PDF.&lt;/p&gt;

&lt;p&gt;For anything scripted, img2pdf is still what I reach for first.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;"Image to PDF" can mean wrapping the original bytes, or decoding and encoding them again. Those are different jobs.&lt;/li&gt;
&lt;li&gt;img2pdf keeps JPEG and PNG losslessly. I checked with a hash, not just a file size.&lt;/li&gt;
&lt;li&gt;Pillow's PDF export re-encodes JPEGs and turns PNGs into JPEGs.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;pdfimages -list&lt;/code&gt; to see what actually landed in the file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you've found a case where img2pdf can't wrap the original encoding, I'd like to hear about it in the comments.&lt;/p&gt;

</description>
      <category>python</category>
      <category>pdf</category>
      <category>linux</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Making Scanned PDFs Searchable with OCRmyPDF and Tesseract</title>
      <dc:creator>OneFileKit</dc:creator>
      <pubDate>Tue, 17 Nov 2026 05:58:56 +0000</pubDate>
      <link>https://dev.to/onefilekit/making-scanned-pdfs-searchable-with-ocrmypdf-and-tesseract-2i04</link>
      <guid>https://dev.to/onefilekit/making-scanned-pdfs-searchable-with-ocrmypdf-and-tesseract-2i04</guid>
      <description>&lt;p&gt;A scanned PDF looks like a normal document, but it behaves like a stack of photographs. You can't search it, you can't copy a paragraph, and a lot of scripts that "extract text" come back empty. I run into this constantly while building file tools: someone emails a "PDF" that is really a photograph of a page. Here's how I tell the difference, and how I add a real text layer with OCRmyPDF and Tesseract.&lt;/p&gt;

&lt;h2&gt;
  
  
  Is it actually a scan?
&lt;/h2&gt;

&lt;p&gt;The quickest check is still &lt;code&gt;pdftotext&lt;/code&gt;, from poppler-utils:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pdftotext input.pdf -
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The trailing &lt;code&gt;-&lt;/code&gt; prints to the terminal. If you only get a blank line, or a form-feed character and nothing else, there is no text layer to read. If you can already highlight and copy sentences in a viewer, you don't need OCR at all.&lt;/p&gt;

&lt;p&gt;A file can also be mixed: a report exported from Word, with a couple of scanned signature pages at the end. That's worth remembering when you choose flags later.&lt;/p&gt;

&lt;h2&gt;
  
  
  OCRmyPDF, not Tesseract alone
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/tesseract-ocr/tesseract" rel="noopener noreferrer"&gt;Tesseract&lt;/a&gt; is the engine that looks at an image and guesses the characters. It's excellent, and you can run it directly on a PNG:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;tesseract page.png stdout
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A PDF is a more awkward input. You'd have to render each page to an image, OCR it, and then stitch a hidden text layer back onto the original page so the file still looks the same. &lt;a href="https://ocrmypdf.readthedocs.io/" rel="noopener noreferrer"&gt;OCRmyPDF&lt;/a&gt; is the tool that does that pipeline well. It uses Tesseract (and a few PDF libraries) and writes a searchable PDF.&lt;/p&gt;

&lt;p&gt;Install it, plus Tesseract and a language pack:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Debian/Ubuntu&lt;/span&gt;
sudo apt install ocrmypdf tesseract-ocr tesseract-ocr-eng
&lt;span class="c"&gt;# macOS&lt;/span&gt;
brew install ocrmypdf
&lt;span class="c"&gt;# or: pip install ocrmypdf&lt;/span&gt;
&lt;span class="c"&gt;# and install Tesseract and Ghostscript yourself&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;tesseract --list-langs&lt;/code&gt; shows what's installed. English is &lt;code&gt;eng&lt;/code&gt;. Add more packs (for example &lt;code&gt;tesseract-ocr-fra&lt;/code&gt; or &lt;code&gt;tesseract-ocr-deu&lt;/code&gt;) if the document isn't in English.&lt;/p&gt;

&lt;h2&gt;
  
  
  The basic command
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ocrmypdf -l eng --output-type pdf input.pdf searchable.pdf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;-l eng&lt;/code&gt; tells Tesseract which language to expect. &lt;code&gt;--output-type pdf&lt;/code&gt; asks for a regular PDF. I set that on purpose: OCRmyPDF's default is to write PDF/A-2b, which is a strict archival flavour of PDF. That's useful when you want an archive copy, but it's a surprising side effect if you only wanted to search the file, and the conversion can fail on unusual inputs. For "make this searchable and leave me alone", &lt;code&gt;--output-type pdf&lt;/code&gt; is the safer default.&lt;/p&gt;

&lt;p&gt;After it finishes, run &lt;code&gt;pdftotext&lt;/code&gt; again. You should see the words. In a PDF viewer you can also select them, even though the page still looks like the original scan. What OCRmyPDF added is an invisible text layer sitting on top of (or rather, behind) the page image.&lt;/p&gt;

&lt;p&gt;If you also want a plain text file, add a sidecar:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ocrmypdf -l eng --output-type pdf --sidecar output.txt input.pdf searchable.pdf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That writes &lt;code&gt;output.txt&lt;/code&gt; as well as the PDF. Handy when you're feeding the text into something else and don't want to parse the PDF a second time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Images, languages and mixed files
&lt;/h2&gt;

&lt;p&gt;OCRmyPDF will also take an image, not only a PDF:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ocrmypdf -l eng --image-dpi 150 --output-type pdf scan.png searchable.pdf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;--image-dpi&lt;/code&gt; matters when the file has no DPI of its own. A photo from a phone often doesn't. If you get a page that's huge or tiny, this is the first flag to check.&lt;/p&gt;

&lt;p&gt;For more than one language on the same page, join the codes with &lt;code&gt;+&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ocrmypdf -l eng+fra --output-type pdf input.pdf searchable.pdf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On mixed documents, I usually start with &lt;code&gt;--skip-text&lt;/code&gt;, which leaves pages that already have a text layer alone and only OCRs the others:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ocrmypdf -l eng --output-type pdf --skip-text mixed.pdf searchable.pdf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;--force-ocr&lt;/code&gt; is the opposite: it OCRs every page, even ones that already have text. That's useful when the existing text is garbled (a bad export, a messy Unicode map). The risk is that you can end up with two text layers on a good page, so I don't use it as the default.&lt;/p&gt;

&lt;p&gt;A few other flags I actually use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;--pages 3-5&lt;/code&gt; to try the process on a few pages before committing a 400-page scan.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--rotate-pages&lt;/code&gt; if some pages were scanned upside down or on their side. OCRmyPDF can detect that and correct it.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-j 4&lt;/code&gt; to run a few pages in parallel.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Accuracy still depends on the scan. Skewed phone photos, tiny type, stamps over words and heavy compression are the usual causes of errors. Proof-read names, amounts and dates. OCR is practical, not perfect.&lt;/p&gt;

&lt;h2&gt;
  
  
  When I don't bother with the terminal
&lt;/h2&gt;

&lt;p&gt;For a pipeline or a folder of archives, I always use OCRmyPDF. When a colleague just needs one contract made searchable, installing Tesseract on their laptop is a lot to ask. I built a free &lt;a href="https://onefilekit.com/pdf-ocr" rel="noopener noreferrer"&gt;PDF OCR&lt;/a&gt; tool on OneFileKit, with no sign-up. You upload the PDF, pick a language and an output format (plain text, Word, or a searchable PDF), and download the result. Uploaded files are deleted after 60 minutes.&lt;/p&gt;

&lt;p&gt;If the PDF already has selectable text and you only want that text out of it, a &lt;a href="https://onefilekit.com/pdf-to-text" rel="noopener noreferrer"&gt;PDF to text&lt;/a&gt; extract is the shorter path. Use OCR when copying from the file gives you nothing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Empty &lt;code&gt;pdftotext&lt;/code&gt; output is the simplest sign that a PDF is only a scan.&lt;/li&gt;
&lt;li&gt;OCRmyPDF wraps Tesseract and writes a hidden text layer; the page still looks the same.&lt;/li&gt;
&lt;li&gt;Prefer &lt;code&gt;--output-type pdf&lt;/code&gt; unless you specifically want PDF/A.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;--skip-text&lt;/code&gt; on mixed files, and &lt;code&gt;--sidecar&lt;/code&gt; if you also want a &lt;code&gt;.txt&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Always read the result. Bad scans produce confident-looking mistakes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What's the largest scanned batch you've had to make searchable? I'd like to know which flags ended up mattering.&lt;/p&gt;

</description>
      <category>pdf</category>
      <category>ocr</category>
      <category>linux</category>
      <category>cli</category>
    </item>
    <item>
      <title>Why Compressing a PDF Sometimes Makes It Bigger (and What Ghostscript's -dPDFSETTINGS Does)</title>
      <dc:creator>OneFileKit</dc:creator>
      <pubDate>Tue, 10 Nov 2026 06:10:36 +0000</pubDate>
      <link>https://dev.to/onefilekit/why-compressing-a-pdf-sometimes-makes-it-bigger-and-what-ghostscripts-dpdfsettings-does-2p41</link>
      <guid>https://dev.to/onefilekit/why-compressing-a-pdf-sometimes-makes-it-bigger-and-what-ghostscripts-dpdfsettings-does-2p41</guid>
      <description>&lt;p&gt;One of the more confusing bug reports I get on my file tools goes like this: "I compressed my PDF and it got bigger." The first time I saw it I assumed a bug. It wasn't. Some PDFs are already about as small as they can get, and running them through a compressor that rebuilds the file can add bytes rather than remove them. Here's why that happens, what Ghostscript's presets actually change, and how I decide which approach to use.&lt;/p&gt;

&lt;h2&gt;
  
  
  What takes up space in a PDF
&lt;/h2&gt;

&lt;p&gt;Before compressing anything, it helps to know what you're compressing. In most real-world PDFs, the size comes from three things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Images.&lt;/strong&gt; Scans, photos and screenshots. In a scanned document they're nearly the whole file.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fonts.&lt;/strong&gt; Embedded font programs, sometimes complete fonts rather than just the glyphs that are used.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Structure and content streams.&lt;/strong&gt; Page drawing instructions, cross-reference tables and metadata. Usually small, but bloated in files produced by some exporters.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You can get a rough picture with poppler's &lt;code&gt;pdfimages&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pdfimages -list input.pdf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This prints every image with its pixel dimensions, encoding, and its resolution as placed on the page (the &lt;code&gt;x-ppi&lt;/code&gt; and &lt;code&gt;y-ppi&lt;/code&gt; columns). A 300 ppi JPEG on every page tells a very different story from a text-only file with 2 MB of embedded fonts.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why a "compressed" PDF can come out larger
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. The images are already compressed well.&lt;/strong&gt; JPEG is lossy. If the source already holds moderate-quality JPEGs, re-encoding them at a similar quality saves almost nothing. If the tool decodes and re-encodes at a higher quality setting than the original, the output gets larger.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. The file was rebuilt, not optimised.&lt;/strong&gt; Ghostscript doesn't edit your PDF. It interprets every page and writes a brand-new file with its own &lt;code&gt;pdfwrite&lt;/code&gt; device. That means fonts get re-embedded and subset its way, and drawing operations get rewritten. For a lean, well-made PDF, the rewritten version can be a little heavier than the original.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Object streams went away.&lt;/strong&gt; Modern PDFs can pack many small objects into compressed "object streams". A tool that writes the old-style layout, one uncompressed object at a time, can make the structure noticeably larger even though the content hasn't changed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Lossless images got converted.&lt;/strong&gt; Some tools switch images between encodings. Flate-compressed screenshots with large flat areas of colour can grow when converted to JPEG, and look worse too.&lt;/p&gt;

&lt;p&gt;So "compress" isn't one operation. It's a choice between rewriting structure losslessly and resampling images lossily, and the right choice depends on the file.&lt;/p&gt;

&lt;h2&gt;
  
  
  What -dPDFSETTINGS actually changes
&lt;/h2&gt;

&lt;p&gt;The usual Ghostscript compression command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 \
   -dPDFSETTINGS=/ebook \
   -dNOPAUSE -dQUIET -dBATCH \
   -sOutputFile=output.pdf input.pdf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;-dPDFSETTINGS&lt;/code&gt; is a bundle of many lower-level parameters. The one that matters most for size is image downsampling resolution:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Preset&lt;/th&gt;
&lt;th&gt;Colour/grey image resolution&lt;/th&gt;
&lt;th&gt;Typical use&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;/screen&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;72 dpi&lt;/td&gt;
&lt;td&gt;Smallest file, on-screen viewing only&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;/ebook&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;150 dpi&lt;/td&gt;
&lt;td&gt;Good balance for reading and email&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;/printer&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;300 dpi&lt;/td&gt;
&lt;td&gt;Office printing&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;/prepress&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;300 dpi, more conservative settings&lt;/td&gt;
&lt;td&gt;Professional print&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;/default&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;General-purpose defaults&lt;/td&gt;
&lt;td&gt;Close to not choosing&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Downsampling only kicks in when an image's effective resolution is above the target (with some tolerance), so &lt;code&gt;/ebook&lt;/code&gt; on a file whose images are already 150 dpi won't shrink those images further. That's case 1 again.&lt;/p&gt;

&lt;p&gt;The presets also affect colour conversion, font embedding and JPEG quality. If you want control instead of a bundle, set the parameters directly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gs -sDEVICE=pdfwrite -dNOPAUSE -dQUIET -dBATCH \
   -dDownsampleColorImages=true -dColorImageResolution=120 \
   -dDownsampleGrayImages=true  -dGrayImageResolution=120 \
   -dColorImageDownsampleType=/Bicubic \
   -sOutputFile=output.pdf input.pdf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Lossless first: qpdf
&lt;/h2&gt;

&lt;p&gt;If a PDF is text-heavy or already has sensible images, I try a lossless structural pass before Ghostscript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;qpdf --object-streams=generate --compress-streams=y \
     --recompress-flate --compression-level=9 \
     input.pdf output.pdf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This packs objects into object streams and recompresses Flate data at the highest level. It never touches image pixels, so quality is identical. Savings are often modest, but you never get a worse-looking file.&lt;/p&gt;

&lt;h2&gt;
  
  
  A script that refuses to make things worse
&lt;/h2&gt;

&lt;p&gt;This is the logic I trust most: try the candidates and keep whichever output is smallest, including the original.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/usr/bin/env bash&lt;/span&gt;
&lt;span class="nb"&gt;set&lt;/span&gt; -euo pipefail
&lt;span class="nv"&gt;in&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"$1"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="nv"&gt;out&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"${2:-compressed.pdf}"&lt;/span&gt;
&lt;span class="nv"&gt;tmp&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$(&lt;/span&gt;mktemp -d&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="nb"&gt;trap&lt;/span&gt; &lt;span class="s1"&gt;'rm -rf "$tmp"'&lt;/span&gt; EXIT

cp &lt;span class="s2"&gt;"$in"&lt;/span&gt; &lt;span class="s2"&gt;"$tmp/original.pdf"&lt;/span&gt;
qpdf --object-streams=generate --compress-streams=y \
     --recompress-flate --compression-level=9 \
     &lt;span class="s2"&gt;"$in"&lt;/span&gt; &lt;span class="s2"&gt;"$tmp/qpdf.pdf"&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
gs -sDEVICE=pdfwrite -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH \
   -sOutputFile=&lt;span class="s2"&gt;"$tmp/gs-ebook.pdf"&lt;/span&gt; &lt;span class="s2"&gt;"$in"&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;

&lt;span class="nv"&gt;best&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$(&lt;/span&gt;ls -S &lt;span class="s2"&gt;"$tmp"&lt;/span&gt;/*.pdf &lt;span class="o"&gt;|&lt;/span&gt; tail -n 1&lt;span class="o"&gt;)&lt;/span&gt;
cp &lt;span class="s2"&gt;"$best"&lt;/span&gt; &lt;span class="s2"&gt;"$out"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Kept $(basename "&lt;/span&gt;&lt;span class="nv"&gt;$best&lt;/span&gt;&lt;span class="s2"&gt;"): $(stat -c %s "&lt;/span&gt;&lt;span class="nv"&gt;$in&lt;/span&gt;&lt;span class="s2"&gt;") -&amp;gt; $(stat -c %s "&lt;/span&gt;&lt;span class="nv"&gt;$out&lt;/span&gt;&lt;span class="s2"&gt;") bytes"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;ls -S&lt;/code&gt; sorts by size with the largest first, so &lt;code&gt;tail -n 1&lt;/code&gt; gives the smallest. On macOS, swap &lt;code&gt;stat -c %s&lt;/code&gt; for &lt;code&gt;stat -f %z&lt;/code&gt;. The &lt;code&gt;|| true&lt;/code&gt; keeps the script going if one tool fails on an unusual file, and in that case the original is still a candidate.&lt;/p&gt;

&lt;p&gt;Always look at the result, not just the number. A &lt;code&gt;/screen&lt;/code&gt; file at 72 dpi can be tiny and also unreadable when printed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Does compression reduce quality?
&lt;/h2&gt;

&lt;p&gt;It depends entirely on which kind of compression you use. Lossless structural compression (qpdf, Flate recompression) doesn't. Downsampling and re-encoding images does, and how visible that is depends on the target resolution and how the document will be used. I wrote a longer explainer on &lt;a href="https://onefilekit.com/blog/does-compressing-a-pdf-reduce-quality" rel="noopener noreferrer"&gt;whether compressing a PDF reduces quality&lt;/a&gt; that goes through it case by case.&lt;/p&gt;

&lt;p&gt;If you'd rather not keep Ghostscript flags in your head for one-off files, I also built a free &lt;a href="https://onefilekit.com/compress-pdf" rel="noopener noreferrer"&gt;Compress PDF&lt;/a&gt; tool on OneFileKit that needs no sign-up.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Find out what's big first: &lt;code&gt;pdfimages -list&lt;/code&gt; tells you most of the story.&lt;/li&gt;
&lt;li&gt;Ghostscript rebuilds the whole file. Great for scans, sometimes worse for lean PDFs.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-dPDFSETTINGS&lt;/code&gt; is mostly about image resolution: 72, 150 or 300 dpi.&lt;/li&gt;
&lt;li&gt;Try a lossless qpdf pass, compare the outputs, and keep the smallest one, which may be the original.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Have you seen a PDF that got bigger after compression? I'd like to know which tool and preset did it.&lt;/p&gt;

</description>
      <category>pdf</category>
      <category>linux</category>
      <category>performance</category>
      <category>cli</category>
    </item>
    <item>
      <title>Merging PDFs from the Command Line: qpdf, pdfunite, Ghostscript and pdftk Compared</title>
      <dc:creator>OneFileKit</dc:creator>
      <pubDate>Tue, 03 Nov 2026 20:56:55 +0000</pubDate>
      <link>https://dev.to/onefilekit/merging-pdfs-from-the-command-line-qpdf-pdfunite-ghostscript-and-pdftk-compared-2g9h</link>
      <guid>https://dev.to/onefilekit/merging-pdfs-from-the-command-line-qpdf-pdfunite-ghostscript-and-pdftk-compared-2g9h</guid>
      <description>&lt;p&gt;I spend a lot of time around PDF files because I build file tools. One request comes up more than almost any other: "I just need these five PDFs as one file." If you live in a terminal, you don't need a GUI for that. Here are the four command-line tools I reach for, what each one is good at, and where each one can trip you up.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. qpdf: my default
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/qpdf/qpdf" rel="noopener noreferrer"&gt;qpdf&lt;/a&gt; is a structural PDF tool. It rewrites the file without re-rendering pages, so text, vector graphics and image quality stay exactly as they were.&lt;/p&gt;

&lt;p&gt;Install:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Debian/Ubuntu&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;qpdf
&lt;span class="c"&gt;# macOS&lt;/span&gt;
brew &lt;span class="nb"&gt;install &lt;/span&gt;qpdf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Merge whole files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;qpdf &lt;span class="nt"&gt;--empty&lt;/span&gt; &lt;span class="nt"&gt;--pages&lt;/span&gt; a.pdf b.pdf c.pdf &lt;span class="nt"&gt;--&lt;/span&gt; merged.pdf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;--empty&lt;/code&gt; starts from a blank document, and everything between &lt;code&gt;--pages&lt;/code&gt; and &lt;code&gt;--&lt;/code&gt; is the list of inputs. You can also pick page ranges per file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;qpdf &lt;span class="nt"&gt;--empty&lt;/span&gt; &lt;span class="nt"&gt;--pages&lt;/span&gt; report.pdf 1-3 appendix.pdf 2,5-z &lt;span class="nt"&gt;--&lt;/span&gt; out.pdf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;z&lt;/code&gt; means the last page, so &lt;code&gt;5-z&lt;/code&gt; is "page 5 to the end". That range syntax is the main reason qpdf is my default: merging and trimming happen in a single command.&lt;/p&gt;

&lt;p&gt;Watch out for: encrypted inputs. If a file has a user password, pass it with &lt;code&gt;--password=...&lt;/code&gt; right after that file's name in the page list.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. pdfunite: the simplest option
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;pdfunite&lt;/code&gt; ships with poppler-utils, which is probably installed already if you use &lt;code&gt;pdftotext&lt;/code&gt; or &lt;code&gt;pdftoppm&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;poppler-utils   &lt;span class="c"&gt;# or: brew install poppler&lt;/span&gt;
pdfunite a.pdf b.pdf c.pdf merged.pdf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The last argument is the output. There are no options worth mentioning, and that's the point. It's great in scripts where you just want everything, in order:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pdfunite &lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;ls &lt;/span&gt;chapter-&lt;span class="k"&gt;*&lt;/span&gt;.pdf | &lt;span class="nb"&gt;sort&lt;/span&gt; &lt;span class="nt"&gt;-V&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt; book.pdf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;sort -V&lt;/code&gt; sorts &lt;code&gt;chapter-10&lt;/code&gt; after &lt;code&gt;chapter-9&lt;/code&gt; rather than after &lt;code&gt;chapter-1&lt;/code&gt;, which plain &lt;code&gt;ls&lt;/code&gt; ordering gets wrong.&lt;/p&gt;

&lt;p&gt;Watch out for: the output filename. If you forget it, pdfunite treats your last input as the output and overwrites it. Keep backups or double-check the argument order. It also can't select page ranges.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Ghostscript: merge and shrink in one pass
&lt;/h2&gt;

&lt;p&gt;Ghostscript doesn't copy pages. It interprets each input and writes a brand-new PDF, which means it can also recompress images while it merges.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gs &lt;span class="nt"&gt;-dBATCH&lt;/span&gt; &lt;span class="nt"&gt;-dNOPAUSE&lt;/span&gt; &lt;span class="nt"&gt;-q&lt;/span&gt; &lt;span class="nt"&gt;-sDEVICE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;pdfwrite &lt;span class="se"&gt;\&lt;/span&gt;
   &lt;span class="nt"&gt;-sOutputFile&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;merged.pdf a.pdf b.pdf c.pdf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add a preset if you also want a smaller file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gs &lt;span class="nt"&gt;-dBATCH&lt;/span&gt; &lt;span class="nt"&gt;-dNOPAUSE&lt;/span&gt; &lt;span class="nt"&gt;-q&lt;/span&gt; &lt;span class="nt"&gt;-sDEVICE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;pdfwrite &lt;span class="se"&gt;\&lt;/span&gt;
   &lt;span class="nt"&gt;-dPDFSETTINGS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/ebook &lt;span class="se"&gt;\&lt;/span&gt;
   &lt;span class="nt"&gt;-sOutputFile&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;merged-small.pdf a.pdf b.pdf c.pdf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;/screen&lt;/code&gt; is the most aggressive, &lt;code&gt;/ebook&lt;/code&gt; is a reasonable middle ground, and &lt;code&gt;/printer&lt;/code&gt; and &lt;code&gt;/prepress&lt;/code&gt; keep more image detail.&lt;/p&gt;

&lt;p&gt;Watch out for: side effects of re-rendering. Form fields, some annotations and links may not survive, and fonts can be re-embedded or subset differently. Ghostscript is also much slower than qpdf on big files. Use it when you want to merge and compress in one go, not for a plain merge.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. pdftk: the classic
&lt;/h2&gt;

&lt;p&gt;pdftk has been around for a long time, and plenty of old scripts and Stack Overflow answers use it. On most distros today you'll get &lt;code&gt;pdftk-java&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;pdftk-java
pdftk a.pdf b.pdf c.pdf &lt;span class="nb"&gt;cat &lt;/span&gt;output merged.pdf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It uses handles for page selection:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pdftk &lt;span class="nv"&gt;A&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;report.pdf &lt;span class="nv"&gt;B&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;appendix.pdf &lt;span class="nb"&gt;cat &lt;/span&gt;A1-3 B2 B5-end output out.pdf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Watch out for: availability. It isn't in Homebrew core, and the Java dependency is heavy if all you want is a merge. If you're starting fresh, qpdf does the same job.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quick comparison
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Tool&lt;/th&gt;
&lt;th&gt;Page ranges&lt;/th&gt;
&lt;th&gt;Keeps original quality&lt;/th&gt;
&lt;th&gt;Can shrink output&lt;/th&gt;
&lt;th&gt;Speed&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;qpdf&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No (structural only)&lt;/td&gt;
&lt;td&gt;Fast&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;pdfunite&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Fast&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ghostscript&lt;/td&gt;
&lt;td&gt;Via &lt;code&gt;-dFirstPage/-dLastPage&lt;/code&gt; per run&lt;/td&gt;
&lt;td&gt;Re-renders&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Slow&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;pdftk&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  A small script I actually use
&lt;/h2&gt;

&lt;p&gt;This merges every PDF in a folder in natural order, then checks the result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/usr/bin/env bash&lt;/span&gt;
&lt;span class="nb"&gt;set&lt;/span&gt; &lt;span class="nt"&gt;-euo&lt;/span&gt; pipefail
&lt;span class="nv"&gt;out&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;1&lt;/span&gt;&lt;span class="k"&gt;:-&lt;/span&gt;&lt;span class="nv"&gt;merged&lt;/span&gt;&lt;span class="p"&gt;.pdf&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="nb"&gt;mapfile&lt;/span&gt; &lt;span class="nt"&gt;-t&lt;/span&gt; files &amp;lt; &amp;lt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt;.pdf | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-v&lt;/span&gt; &lt;span class="s2"&gt;"^&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;out&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;$"&lt;/span&gt; | &lt;span class="nb"&gt;sort&lt;/span&gt; &lt;span class="nt"&gt;-V&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
qpdf &lt;span class="nt"&gt;--empty&lt;/span&gt; &lt;span class="nt"&gt;--pages&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;files&lt;/span&gt;&lt;span class="p"&gt;[@]&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;--&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$out&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
qpdf &lt;span class="nt"&gt;--check&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$out&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; /dev/null &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"OK: &lt;/span&gt;&lt;span class="k"&gt;${#&lt;/span&gt;&lt;span class="nv"&gt;files&lt;/span&gt;&lt;span class="p"&gt;[@]&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; files -&amp;gt; &lt;/span&gt;&lt;span class="nv"&gt;$out&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;qpdf --check&lt;/code&gt; is worth running after any merge. It catches broken cross-reference tables before you send the file to someone else.&lt;/p&gt;

&lt;h2&gt;
  
  
  When the terminal isn't an option
&lt;/h2&gt;

&lt;p&gt;Not everyone you help will have a shell, Homebrew or apt. When a colleague or family member asks me to merge PDFs, installing qpdf on their laptop is overkill. For that case I built a free browser tool that needs no sign-up: &lt;a href="https://onefilekit.com/merge-pdf" rel="noopener noreferrer"&gt;Merge PDF on OneFileKit&lt;/a&gt;. You drop the files in, reorder them and download a single PDF.&lt;/p&gt;

&lt;p&gt;For anything scripted, repeatable or running on a server, I still use qpdf.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Plain merge, with or without page ranges:&lt;/strong&gt; qpdf&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Quickest merge with no options:&lt;/strong&gt; pdfunite&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Merge and make it smaller:&lt;/strong&gt; Ghostscript with &lt;code&gt;-dPDFSETTINGS&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Maintaining old scripts:&lt;/strong&gt; pdftk&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What do you use? If there's a tool I've missed, especially on Windows, I'd like to hear about it in the comments.&lt;/p&gt;

</description>
      <category>pdf</category>
      <category>cli</category>
      <category>linux</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
