viernes, 24 de abril de 2020

Convert a pdf presentation to another pdf with everything a bitmap and crisp images

Sometimes pdf presentations rely on specific fonts, etc which may be unavailable on some computers.

Sometimes, a given pdf is very slow to show up on certain computers.

A solution can be to convert everything to bitmaps. This increases file size but makes rendering straightforward. With a trick that keeps the png format on the resulting pdf, the results are as crisp as in the original





#!/bin/bash

# Convert PDF file to individual pdf files with given resolution (96 DPI)
# without losing definition.

NPAGES=$(pdfinfo $1 | grep Pages | awk '{print $2}')

echo $NPAGES

for i in $(seq $NPAGES); do
  echo Processing page $i
  echo Generating "__FILE$i".pdf
  pdftk  A=$1 cat "A$i" output - | convert -density 96 - png:- | img2pdf -o "__FILE$i".pdf -
#              ^                      ^                   ^          ^
#              |                      |                   |          lossless conversion to pdf
#              |                      |                   specify PNG
#              |                      convert to png
#              extract page
done

# The last part comes from
# https://unix.stackexchange.com/questions/42856/how-can-i-convert-a-png-to-a-pdf-in-high-quality-so-its-not-blurry-or-fuzzy