3

I want to convert a bunch of Webp images to individual PDFs. I'm able to do it with this command:

parallel convert '{} {.}.pdf' ::: *.webp

or I can use this FFMPEG command:

find ./ -name "*.webp" -exec dwebp {} -o {}.pdf \;

However during the process of conversion the Webp files are decoded and the resultant PDFs have a much bigger file size. When I use the above commands for JPG-to-PDF conversion the PDF size is reasonably close to the JPG image size.

This command works fine with JPGs, but the program img2pdf doesn't work with the Webp format:

find ./ -name "*.jpg" -exec img2pdf {} -o {}.pdf \;

I also tried Webp-to-PDF conversion with this online service, but the PDF was huge.

How can I keep the PDF size down to the Webp file size?

whitewings
  • 2,377
  • 7
  • 32
  • 53
  • One possibility is to first convert the webp images to jpg, then to pdf. This might reduce the quality a little, but should work. – meuh Jan 15 '18 at 21:25
  • Yes this would work, but I am specifically asking how to do this with webp files. – whitewings Jan 16 '18 at 16:01
  • You will only get a small size pdf if you go via an intermediate jpg stage, as jpg compression is supported natively in pdf, but webp is not. – meuh Jan 16 '18 at 16:03
  • Could I get webp compression supported in pdf through some other program? – whitewings Jan 16 '18 at 18:22
  • Yeah, I guess until pdf supports webp natively (or someone writes a PS implementation of the webp decode in a way small enough to include in a PDF!) this won't be possible without losing significant quality – artfulrobot Mar 21 '20 at 12:52

1 Answers1

1

Why not just use Imagemagick and Ghostscript?

convert img.webp img.pdf
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dPDFSETTINGS=/ebook -sOutputFile=img-small.pdf img.pdf

In my test with your sample file I got a pdf result of about 3.2 MB.

EDIT

You could follow these instructions on Ubuntu to make sure that imagemagick was built with webp. Install this package for Windows or on macos do this:

brew install webp
brew install imagemagick
Alireza S.N
  • 103
  • 4
Nothingismagick
  • 226
  • 1
  • 6
  • I have ImageMagick already and have tried some commands. Your command takes a series of webp images that amount to 2mb and creates a 100mb pdf. Even when I reduce the quality to 80 and the density to 50x50. I want a command that will give me a pdf that is roughly equal in size to the source webp images. – whitewings Jan 16 '18 at 15:59
  • @user8547 can you post a link to webp file example you are working on? – Nothingismagick Jan 16 '18 at 17:40
  • Here's a link. When I try all the methods of converting this to pdf it comes out around 150mb. https://cdn.pbrd.co/images/H3e1vmd.webp – whitewings Jan 16 '18 at 18:20
  • @user8547 updated with working sample script. – Nothingismagick Jan 16 '18 at 20:05
  • I have ImageMagick 64 bit on Windows 10, so I needed to use `magick` instead of `convert` as a command. With the Ghostscript command `"C:\Program Files\gs\gs9.22\bin\gswin64c.exe" -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dPDFSETTINGS=/eBook -sOutputFile=imgsmall.pdf img.pdf` i get the error `Unrecoverable error: undefined in get Operand stack: --nostringval-- eBook` – whitewings Jan 17 '18 at 00:08
  • I tried your command first on the Ubuntu Subsystem for Windows, but had no luck getting it to work. I got this long error: `Unknown device: pdfwrite-dNOPAUSE ./base/gsicc_manage.c:1088: gsicc_open_search(): Could not find default_gray.icc | ./base/gsicc_manage.c:1708: gsicc_set_device_profile(): cannot find device profile Unrecoverable error: unknownerror in .special_op Operand stack: defaultdevice Unrecoverable error: undefined in .uninstallpagedevice Operand stack: defaultdevice` – whitewings Jan 17 '18 at 00:09
  • 1
    My bad - autocorrect bit again, it should be ebook without the camel case. I updated the answer for you – Nothingismagick Jan 17 '18 at 01:31