Problem converting pdf to jpg via ImageMagick

I have a problem converting pdf to jpg via ImageMagick.

The following command works successfully by producing a pdf file from a jpg file.

convert.exe -density 300 "C:\TEMP\testing.jpg" -depth 8 "C:\TEMP\testing.pdf"

Open in new window

However, reversing the process by converting the pdf file back to a jpg file does not seem to work.
Errors are not shown, but no output file is produce.

convert.exe -density 300 "C:\TEMP\testing.pdf" -depth 8 "C:\TEMP\testing.jpg"

Open in new window

I have installed the latest imagemagick (ImageMagick-7.0.8-37-Q16-x64-dll.exe) from and ghostscript (9.27) from both for Windows x64.

Your kind help would be appreciated.
Thomas PaikDoing IT work in LawAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tony GimenezCybersecurity Professional & Ethical HackerCommented:
You might want to use Zamzar for online file conversion instead if you haven't heard of it and if it suits your needs.
David FavorLinux/LXD/WordPress/Hosting SavantCommented:
Consider what you're asking... Very complex, if you start with a text component + must regenerate a text component, when you reassemble your images back into a PDF.

Here's how.

You can do this...

# convert PDF to a collection of JPG images
imac> convert -density 300 2019-02-30-ovh-tos.pdf 2019-02-30-ovh-tos.jpg

# which produces this set of JPG images
imac> ls -1 *.jpg

# Now stitch all these JPG images back into a PDF, with text component lost
imac> convert "2019-02-30-ovh-tos-*.jpg" -quality 100 foo.pdf

Open in new window

If you require regenerating your PDF with a text component, open another question about this + I'll dig out my script to run tesseract + stitch in the text component.
Joe WinogradDeveloperCommented:
Hi Thomas,

My Experts Exchange article shows how to do it with GraphicsMagick:

Create an image (BMP, GIF, JPG, PNG, TIF, etc.) from a multi-page PDF

The call is the same with ImageMagick — simply replace gm.exe convert with magick.exe convert.

I also wrote these other articles about GraphicsMagick that you may find helpful:

Reduce the file size of many JPG files in many folders via an automated, mass, batch compression method

Create a PDF file with Contact Sheets (montage of thumbnails) for all JPG files in a folder and each of its subfolders using an automated, batch method

Convert a multi-page PDF file into multiple image files

As you probably know, GraphicsMagick is a fork of ImageMagick, and while the products have gone their separate ways since the fork, they retain many similarities. I prefer GraphicsMagick, but both are very good, and ImageMagick is easier to use in programs/scripts, as it has fewer run-time dependencies than GraphicsMagick. For your requirement, both will work fine. Regards, Joe
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Thomas PaikDoing IT work in LawAuthor Commented:
To Tony: I am looking for a windows command line conversion solution. Thanks anyways.
To David: I tried your suggestion but it does not seem to solve the problem at hand. Thanks for the tip.
To Joe: I tried GraphicsMagick. It is faster, but it still does not seem to solve the problem at hand, which is to convert pdf to jpg. Still getting no output file. Perhaps these conversion applications have problems with reliably reading source pdf files?

WORKS FINE: magick.exe convert C:\TEMP\testing.jpg C:\TEMP\testing.pdf
DOES NOT WORK: magick.exe convert C:\TEMP\testing.pdf C:\TEMP\testing.jpg
David FavorLinux/LXD/WordPress/Hosting SavantCommented:
The only real tricky part is...

1) You start with a text component + must reintegrate the text component... or...

2) You must generate a text component.

For #1, you can likely get away with Poppler (what I use), so something like this to generate your text component.

pdftotext -enc ASCII7 -nopgbrk -layout infile - > oufile

Open in new window

For #2, use tesseract which is a phenomenal tool. Be sure to load the correct language pack targeting your output language, as there are many language packs.
David FavorLinux/LXD/WordPress/Hosting SavantCommented:
You said, "I tried your suggestion but it does not seem to solve the problem at hand. Thanks for the tip."

1) Create a directory + cd to the directory.

2) Place your .pdf file in this directory.

3) Run...

convert -density 300 your-file.pdf your-file.jpg

Open in new window

Cut + paste any error output that convert emitted.

4) Now show what happened...

ls -1 *

Open in new window

And post the output of ls.

5) Based on what happened will determine next step.
David FavorLinux/LXD/WordPress/Hosting SavantCommented:
You can also attach a copy of your .pdf file as there may be a problem with it's structure.

Many .pdf files are malformed. If this is the case an extra step to fix any malformation will be required.

Random Tip: For malformed .pdf files this can work many times to extract the JPG page images...

pdftohtml -c your-file.pdf

Open in new window

This splits out all the images embedded in a .pdf file, even for badly mangled/malformed .pdf files.
Joe WinogradDeveloperCommented:
> To Joe: I tried GraphicsMagick. It is faster, but it still does not seem to solve the problem at hand, which is to convert pdf to jpg.

Did you use the command in my article? ImageMagick and GraphicsMagick both work perfectly here. Please post the command that you're using.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Thomas PaikDoing IT work in LawAuthor Commented:
Thank you very much for helping out.
I finally got it working!
ImageMagick seems not fully compatible with my system.

Here are the commands I tested on Windows 10 x64 (CMD.exe)

C:\TEMP>gm.exe convert testing.pdf testing.jpg

DOES NOT WORK(no jpg file produced):
C:\TEMP>convert.exe testing.pdf  testing.jpg
C:\TEMP>magick.exe convert testing.pdf testing.jpg

However, the jpg quality seems mediocre.
Text readability is not good.
What options can I add to the gm.exe command, in order to increase text resolution?
Joe WinogradDeveloperCommented:
I'm leaving my office now for a few hours. Will check back into the thread when I return to see how you're doing. In the meantime, the GraphicsMagick -density option will probably solve your problem:

gm.exe convert -density 400 testing.pdf testing.jpg

I put in 400 there, but try various values until you get the quality/size trade-off that you like. Regards, Joe
Thomas PaikDoing IT work in LawAuthor Commented:
Thanks to all!
Joe WinogradDeveloperCommented:
You're welcome, Thomas, I'm glad that worked for you. Regards, Joe
Thomas PaikDoing IT work in LawAuthor Commented:
Further to my question, how do I convert only page 1 of the pdf file?

What option do I use after gm.exe convert?
Joe WinogradDeveloperCommented:
> how do I convert only page 1 of the pdf file?

To convert any single page, put the page number in square brackets after the name of the PDF file, but page numbering starts at 0, not 1. For example, to convert the first page of the file:

gm.exe convert -density 400 testingmultipage.pdf[0] testing.jpg

To convert the fifth page of the file:

gm.exe convert -density 400 testingmultipage.pdf[4] testing.jpg

Regards, Joe
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Images and Photos

From novice to tech pro — start learning today.