xianghuzhao / pdf-bookmark

Import and export pdf bookmark
MIT License
38 stars 8 forks source link

Bookmarking a PDF also shrinks its file size and compresses images #3

Open madamadami opened 3 years ago

madamadami commented 3 years ago

I like your script and used it quite a lot recently. Unfortunately, the PDFs get compressed pretty much and so images lose a lot of quality. How can I avoid recompression?

smfukaya commented 2 years ago

@madamadami, perhaps you could add the Ghostscript settings (highlighted in bold) posted Kurt Pfeifle is his answer to Use Ghostscript, but tell it to not reprocess images?.

So the complete Ghostscript commandline that could make you happy should read:

gs \ -o book.pdf \ -sDEVICE=pdfwrite \ -dColorConversionStrategy=/LeaveColorUnchanged \ -dDownsampleMonoImages=false \ -dDownsampleGrayImages=false \ -dDownsampleColorImages=false \ -dAutoFilterColorImages=false \ -dAutoFilterGrayImages=false \ -dColorImageFilter=/FlateEncode \ -dGrayImageFilter=/FlateEncode \ title.pdf \ content.pdf

Find where your pdf-bookmark was installed with the following command: pip show pdf-bookmark

Name: pdf-bookmark Version: 1.1.0 Summary: PDF Bookmark Import and Export Home-page: https://github.com/xianghuzhao/pdf-bookmark Author: Xianghu Zhao Author-email: xianghuzhao@gmail.com License: MIT Location: c:\LOCATION\python\python310\lib\site-packages Requires: Required-by:

Edit the "c:\LOCATION\python\python310\lib\site-packages\pdf-bookmark.py" (change the path accordingly) file, find the call command to ghostscript executable, add the above settings to the arguments array and save the file.

    call(['gs', '-dBATCH', '-dNOPAUSE', '-sDEVICE=pdfwrite',
          '-dColorConversionStrategy=/LeaveColorUnchanged', '-dDownsampleMonoImages=false', '-dDownsampleGrayImages=false', '-dDownsampleColorImages=false',
          '-dAutoFilterColorImages=false', '-dAutoFilterGrayImages=false', '-dColorImageFilter=/FlateEncode', '-dGrayImageFilter=/FlateEncode',
          '-sOutputFile={}'.format(output_pdf),
          pdfmark_noop,
          pdf,
          pdfmark_restore,
          pdfmark_file,
          pdfmark_pagemode])

Run the pdf-bookmark and check if the image quality suits your need.