javaghostscriptghost4j

how to use GhostScript to convert a pdf to jpg


It bothered me a whole day afternoon. I encounter many problems and by now i cannot overcome them.

my code:

  public void pdf2jpg(){
    try {
        File pdfFile =new File("c://tmp//1.pdf");
        PDFDocument document = new PDFDocument();
        document.load(pdfFile); 

        SimpleRenderer renderer = new SimpleRenderer(); 
        renderer.setResolution(300);  

        List<Image> images = renderer.render(document); 

         for (int i = 0; i < images.size(); i++) {  
             Image img= images.get(i);
                ImageIO.write((RenderedImage)img, "jpg",  new File(i+".jpg"));
         } 

    } catch (IOException | RendererException | DocumentException e) {
        e.printStackTrace();
    }   

  }

My box: Windows 7, jdk:1.7.0_45(64bit), GPL ghostscript 9.0.4. when I use ghost4j 0.4.4, I got error "net.sf.ghost4j.renderer.RendererException: net.sf.ghost4j.GhostscriptException: Cannot initialize Ghostscript interpreter. Error code is -20"

on List<Image> images = renderer.render(document);

Some thread here mentions it's about ghost4j version. so i change to 0.4.6, error 20 disappears, but comes Warning: An error occurred while reading an XREF table. on the same sentence.

I cannot figure out how to get out of this 'mud',

Thanks very much for your help.


Solution

  • I'd guess that the returned bitmap is simply too large for memory, given that you get an out of memory error.

    You should try using Ghostscript directly from the command line for 2 reasons, firstly you will be able to see if there is a real error message about the xref, which would indicate your PDF file is damaged, secondly you might reasonably just run a shell command to invoke GhostScript to render the PDF directly to JPEG rather than going through an in-memory bitmap. Its probably faster apart from anything else.

    gswin32c -sDEVICE=jpeg -o out.jpg input.pdf