PDF to JPEG with ImageMagick and Ghostscript

Post Reply
mkis
Member
Posts: 23
Joined: Fri Oct 21, 2011 3:19 pm
Location: Ahaus, Germany
Contact:

PDF to JPEG with ImageMagick and Ghostscript

Post by mkis »

Hello all,



I've got a problem with converting PDF to JPG with ImageMagick (and Ghostscript)

There is a flow with the "execute command". The program that is used is convert from ImageMagick, simply using the "%1" "%2" arguments.

output File Extension is jpg.



Now every PNG, TIF or any other image is running perfect through this flow.

But any PDF File is send to null by switch...

Using an Switch Script is also NOT working!



The same command manual used in the terminal is working!



Does anyone have a solution for that?

Or have an alternative tool (exept Acrobat or SIPS) to get good quality jpgs from PDFs?



Thanks in advance!



Michael
dkelly
TOP CONTRIBUTOR
Posts: 658
Joined: Mon Nov 29, 2010 8:45 pm
Location: Alpharetta GA USA
Contact:

PDF to JPEG with ImageMagick and Ghostscript

Post by dkelly »

Hello, if you are using "Execute command" configurator then you should be able to select one of the Output properties that supports "Copy input job" == yes option.
mkis
Member
Posts: 23
Joined: Fri Oct 21, 2011 3:19 pm
Location: Ahaus, Germany
Contact:

PDF to JPEG with ImageMagick and Ghostscript

Post by mkis »

Hello Dwight,



thanks for your answer, I've tried any possible options in the "Execute command" and always the same error.

Job send to null...



With a similar SwitchScript in JavaScript an error occures, the gs command can't find the file to convert.



But from the terminal with the same arguments the converting works fine.



Regards,



Michael
dkelly
TOP CONTRIBUTOR
Posts: 658
Joined: Mon Nov 29, 2010 8:45 pm
Location: Alpharetta GA USA
Contact:

PDF to JPEG with ImageMagick and Ghostscript

Post by dkelly »

I know it will work with a JS script because I've done it many times. You have to create a job folder, move the PDF and JPEG file into it and then call SendToSingle() with the job folder.
mkis
Member
Posts: 23
Joined: Fri Oct 21, 2011 3:19 pm
Location: Ahaus, Germany
Contact:

PDF to JPEG with ImageMagick and Ghostscript

Post by mkis »

OK, then I think I'll check the ImageMagick Installation first.

Maybe there is something broken....



Thanks for your help!



Michael
mkis
Member
Posts: 23
Joined: Fri Oct 21, 2011 3:19 pm
Location: Ahaus, Germany
Contact:

PDF to JPEG with ImageMagick and Ghostscript

Post by mkis »

Hello again.



I'm sorry, but I didn't get it.

In my script, the ImageMagick wont't work, I always get an error "gs: can't find the image..."



So Ghostscript won't get thr image from ImageMagick, because switch already runs in an error?



So here's my script, maybe someone can help...



Thanks!!!!





function jobArrived( s : Switch, job : Job )

{

var theProcessPath = s.getPropertyValue( "argProcessPath" );

var thePixelWidth = s.getPropertyValue( "argPixelWidth" );

var theOutputFormat = s.getPropertyValue( "argOutputFormat" );

var theArguments = new Array();

// var theTempPath = job.createPathWithName( job.getNameProper() );

var theOutputFile = "/path_without_spaces" + job.getNameProper() + "." + theOutputFormat;



theArguments.push( theProcessPath );

theArguments.push( job.getPath() );

theArguments.push( theOutputFile );

theArgumentsString = theArguments.join(" ");



var returnCode = Process.execute( theArgumentsString );



job.log (2, theArgumentsString );



if( Process.stderr != "" ) {

job.log( 3, "Prozess-Fehler: " + Process.stderr );

}



if( new File( theOutputFile ).exists ) {

job.sendToSingle( theOutputFile );

job.sendToSingle( job.getPath() );

} else {

job.log(3, "IM-Datei existiert nicht!: " + Process.stderr );

}

}
roope
Newbie
Posts: 1
Joined: Wed Jul 20, 2011 2:47 pm

PDF to JPEG with ImageMagick and Ghostscript

Post by roope »

Hi Michael,



Can you still try execute command with GhostScript? Here is sample settings which are working for me.



Command or Path: C:Program Filesgsgs9.06bingswin32c.exe



Arguments: -dSAFER -dBATCH -dNOPAUSE -r300 -dJPEGQ=80 -dDOINTERPOLATE -dMaxBitmap=500000000 -dUseTrimBox -dGraphicsAlphaBits=4 -dTextAlphaBits=4 -sDEVICE=jpeg -o "%2" "%1"



Output: File at path



Copy input job: No



Output extension: JPEG (*.jpg; *.jpeg)



Fail if exit code is: Nonzero



Have you done some conversions with ImageMagic from png or tiff to jpg? Can you give some sample settings?



-Robert
mkis
Member
Posts: 23
Joined: Fri Oct 21, 2011 3:19 pm
Location: Ahaus, Germany
Contact:

PDF to JPEG with ImageMagick and Ghostscript

Post by mkis »

Hi Robert,



thanks for your answer, this gives me a temporary solution for my problem.

I'll use your direkt command for PDF Files now, thanks!



With ImageMagick I'm using

Arguments: "%1" -compress JPEG -quality 99 "%2"

The rest as for Ghostscript...



Regards,

Michael
dkelly
TOP CONTRIBUTOR
Posts: 658
Joined: Mon Nov 29, 2010 8:45 pm
Location: Alpharetta GA USA
Contact:

PDF to JPEG with ImageMagick and Ghostscript

Post by dkelly »

Hello, just use the arguments array directly instead of combining them into a string.



mkis wrote: var theArguments = new Array();

var theOutputFile = "/path_without_spaces" + job.getNameProper() + "." + theOutputFormat;



theArguments.push( theProcessPath );

theArguments.push( job.getPath() );

theArguments.push( theOutputFile );

var returnCode = Process.execute( theArguments );





}
freddyp
Advanced member
Posts: 1008
Joined: Thu Feb 09, 2012 3:53 pm

Re: PDF to JPEG with ImageMagick and Ghostscript

Post by freddyp »

As I was searching for something else I stumbled over this very old post and I saw that it does not have a conclusive answer and I thought I would add it now as it is still relevant today and possibly for other applications than Imagemagick too.

When running "Execute command" the application is not run in the same environment as when you are running it in Terminal/Command prompt so you cannot rely on all environment variables (esp. PATH) to be the same, even to be defined. And that is where the problem lies with Imagemagick/Ghostscript. Imagemagick calls Ghostscript by simply calling "gs" or "gsc" and when the PATH variable does not include the location of Ghostscript it will fail.

Solution: do not run Imagemagick directly in "Execute command", but run a shell script/BAT file so you can set the PATH variable correctly before running Imagemagick.
Jiyahana
Newbie
Posts: 9
Joined: Wed Jan 03, 2024 6:06 pm
Location: India
Contact:

Re: PDF to JPEG with ImageMagick and Ghostscript

Post by Jiyahana »

Hey @mkis you can get better understanding from https://blog.enfocus.com/how-to-reduce- ... ng-quality on the other hand you can explore the online application for compression such as compress jpeg.
elsieaortega
Newbie
Posts: 2
Joined: Tue Feb 27, 2024 8:02 am

Re: PDF to JPEG with ImageMagick and Ghostscript

Post by elsieaortega »

'd be glad to assist you with converting PDFs to JPGs using ImageMagick. Here's a comprehensive response combining the best aspects of previous responses and addressing potential issues:

Understanding the Issue:

Command Execution: The problem likely lies in how the "execute command" flow within your system handles the PDF conversion specifically. While convert works flawlessly in the terminal, its integration with your flow might be causing issues.
Solutions:

Verbose Mode:

Add the -verbose flag to the convert command to gain detailed output about the conversion process. This will help identify any errors encountered during execution.
convert -verbose "your_pdf.pdf" "output.jpg"
Temporary File Handling:

If your flow involves temporary files, ensure they are created correctly with the appropriate permissions and accessible by the convert process.
Use tools like mktemp or tmpfile to create temporary files with proper handling.
Consider explicitly specifying the full path to the temporary file in the convert command.
Ghostscript Integration and Troubleshooting:

ImageMagick relies on Ghostscript (gs) for PDF rendering. If gs is not installed or configured correctly, conversion issues might arise.
Verify the installation of gs using your system's package manager (e.g., apt-get install ghostscript on Ubuntu/Debian).
If gs is found, check its version and configuration using commands like gs --version and gs -h.
Ensure environment variables like PATH are set correctly to locate gs executables.
If gs is still causing problems, consider alternative rendering engines like pdftoppm or pdftocairo, though they might require additional setup and have limitations.
Post Reply