Skip to content

Latest commit

 

History

History
executable file
·
173 lines (143 loc) · 10.9 KB

readme.org

File metadata and controls

executable file
·
173 lines (143 loc) · 10.9 KB

Background

Depending on the pdf creator/engine used, a .pdf file may include content irrelevant for reading by a human. An example is the inclusion of complete sets of fonts though only a few glyphs are (or only one is) used on the “electronic paper”. To print the .pdf with ghostscript again into a .pdf may reduce the file size e.g., prior to organize a publication in a reference manager.

By far, this bash script does not claim to be the first one collecting bits and bolts to address the issue. It rather serves as an aide-memoire of finds encountered earlier, and to moderate ghostscript in Linux accordingly. Within reason, the snippets were joined as provided; thus, the credit belongs to those already in the field.

Intended Use

One use consists of 1) the provision of the executable bit (chmod +x pdf_reprint.sh), and 2) to set an alias in your .bashrc file. Then, the functionality described below is available all across your system.

  • Else, to reprint the .pdf while retaining the color, run either one of the following commands
    bash ./pdf_rewrite.sh --reprint input.pdf
    bash ./pdf_rewrite.sh -r input.pdf
    bash ./pdf_rewrite.sh --colour input.pdf
    bash ./pdf_rewrite.sh --color input.pdf
    bash ./pdf_rewrite.sh -c input.pdf
        

    to replace the original file input.pdf by a new version of smaller file size. There will be a short note if the attempt was successful; and if so, the improvement compared to the original input.pdf is reported (percentage). Though you may process the file multiple times with this method, savings in file size often quickly converge to be insignificant in comparison to the remaining file size.

    The credit for the underlying approach and implementation belongs to Evan Langlois.[fn:Langlois]

  • Often, an additional reduction of file size may be obtained by reprinting the .pdf in gray-scale only. Either one of the following commands to the script is equivalent
    bash ./pdf_rewrite.sh --gray input.pdf
    bash ./pdf_rewrite.sh --grey input.pdf
    bash ./pdf_rewrite.sh -g input.pdf
        

    to replace file input.pdf by its rewritten form. The credit for this approach belongs to user slm on the Unix stackexchange.[fn:slm]

  • To process multiple .pdf, a for-loop in your shell could follow the pattern of
    for file in *.pdf
    do
        echo "$file"
        bash ./pdf_rewrite.sh -r "$file"
    done
        

    This equally provides you a brief progress report, too.

Note, this script’s primary aim is to obtain a file of small file size, e.g., as an attachment of an email while retaining the text easy to read and – if present – to retain a text layer searchable. Especially the reprint in half-tones however may render illustrations less intelligible. It is up to the creators of figures to use easy discernible markers in diagrams, as well as to use color scales suitable for the color blind, and safe for this mimicked “photocopying”. For the later, a service like https://colorbrewer2.org/ may guide your selection.

Keep a backup of the .pdf to be processed. Though the script may report problems while processing the data (or even crash, which may destroy the .pdf), it is not a PDF validator such as e.g., veraPDF.[fn:verapdf]

Benchmark

Initially written for Linux Xubuntu 18.04.3 LTS and ghostscript (version 9.26), the script is known working well e.g., with Debian 13/trixie (currently testing) and GPL Ghostscript (version 10.02.1 published by 2023-11-01).

  • File link2web.pdf was compiled with pdfLaTeX based on an example provided by www.texample.net. This .pdf contains a color figure and link to an external reference. Note, the simplification into half-tones (option -g) affects the document printed, depending on the pdf viewer used, the box around the link may remain colored for the display on screen.
  • The performance of the utility was tested on a couple of recent publications in chemistry. To ease a potential replication, publications used for the bench marke are – within reason – available open access/CC.

In the table below, savings computes the difference of the file size prior and after the processing with either option, then reports this change as percentage in respect to the file size of the originally submitted file after a single run of optimization.

Typically, the simple reprint -r retaining the color is the fastest approach to reduce most of the file size in one run and hence already good enough. How much file size is saved seems to vary not only by the relatice amount of special (mathematical) characters, but among journals by the same publisher; see for instance the small savings for a reprint for J. Appl. Cryst. vs. Helv. Chim. Acta both published by Wiley.

<l><r><r><r><r><r><r>
sourcepublisheroriginalreprint -rsaved %reprint -gsaved %
2023ACR3640ACS7.0 MB4.5 MB35.73.2 MB54.3
2023ACR3654ACS2.4 MB1.6 MB33.31.6 MB33.3
2023CrystGrowthDes8469ACS3.7 MB0.9 MB75.70.9 MB75.7
2024CrystGrowthDes71ACS10.5 MB1.5 MB85.71.4 MB86.7
2023CRV12135ACS9.4 MB6.1 MB35.15.3 MB43.6
2023CRV13291ACS25.5 MB4.0 MB84.33.7 MB85.5
2023CRV13713ACS12.0 MB4.5 MB62.54.2 MB65.0
2023JCE4674ACS2.8 MB2.2 MB21.42.1 MB25.0
2023JCE4728ACS2.6 MB1.0 MB61.51.0 MB61.5
2023JOC16679ACS4.7 MB3.2 MB31.93.0 MB36.2
2023JOC16719ACS9.9 MB2.4 MB75.82.1 MB78.8
2023OL9002ACS2.4 MB1.2 MB50.01.1 MB54.2
2023OL9243ACS2.2 MB1.4 MB36.41.4 MB36.4
2023Tetrahedron133750Elsevier1.2 MB1.0 MB16.70.6 MB50.0
2024Tetrahedron133787Elsevier1.9 MB1.8 MB5.31.6 MB15.8
2023TL154433Elsevier831 kB721 kB13.2497 kB40.2
2024TL154885Elsevier1.6 MB0.9 MB43.80.9 MB43.8
2024PCCP713RSC1.0 MB1.0 MB0.00.5 MB50.0
2024PCCP770RSC2.3 MB2.1 MB8.70.8 MB65.2
2024TheorChemAcc4Springer1.7 MB0.8 MB52.90.7 MB58.8
2023TheorChemAcc133Springer1.7 MB1.0 MB41.21.0 MB41.2
2024JSulfurChem138Taylor & Francis469 kB248 kB47.1247 kB47.3
2023JSulfurChem269Taylor & Francis5.6 MB2.9 MB48.22.0 MB64.3
2023Synthesis3777Thieme976 kB936 kB4.1528 kB45.9
2023Synthesis3947Thieme2.2 MB2.2 MB0.01.9 MB13.6
2024ACIEe202310983Wiley877 kB800 kB8.8507 kB42.2
2024ACIEe202314446Wiley2.5 MB2.4 MB4.01.2 MB52.0
2023HCAe202300110Wiley10.4 MB5.5 MB47.12.9 MB72.1
2023HCAe202300154Wiley10.4 MB5.7 MB45.22.2 MB78.8
2023JApplCryst1618Wiley1.1 MB1.1 MB0.00.9 MB18.2
2023JApplCryst1639Wiley2.8 MB2.7 MB3.61.2 MB57.1
link2web.pdfpdflatex38.0 kB9.8 kB74.29.8 kB74.2

Footnotes

[fn:Langlois] https://tex.stackexchange.com/questions/18987/how-to-make-the-pdfs-produced-by-pdflatex-smaller?rq=1

[fn:slm] https://unix.stackexchange.com/questions/93959/how-to-convert-a-color-pdf-to-black-white

[fn:verapdf] https://openpreservation.org/tools/verapdf/