将PDF页面转换为Java-GAE上的JPG

6
我正在寻找一个开源的Java库,可以在服务器端将PDF单页渲染为JPG或PNG格式。不幸的是,它不能使用除以下三个类之外的任何java.awt.*类:
  • java.awt.datatransfer.DataFlavor
  • java.awt.datatransfer.MimeType
  • java.awt.datatransfer.Transferable
如果有任何方法,一个小的代码片段就会很棒。

1
http://stackoverflow.com/questions/11513841/appengine-conversion-api-java展示了如何使用Google转换API。*但是*有一个问题。该API将在11月份被删除。也许您可以向Google询问任何替代方法的提示。 - halex
1
是的,我看到了。但正如你所写的,支持很快就会停止。否则它本来是完美的。我会尝试从谷歌上获取一些信息。 - Bommelmutze
嗨,你有找到其他可以进行相同转换的工具吗?我也在寻找类似的功能。我知道我可以使用Google Drive从小于25MB的PDF中请求图像。但是我需要它能够处理更大的文件。 - DavidVdd
2个回答

0

您可以使用 Apache PDFBox API 来实现此目的,并使用以下代码逐页将两个 PDF 转换为 JPG。

public  void convertPDFToJPG(String src,String FolderPath){

           try{
               File folder1 = new File(FolderPath+"\\");
               comparePDF cmp=new comparePDF();
               cmp.rmdir(folder1);

           //load pdf file in the document object
           PDDocument doc=PDDocument.load(new FileInputStream(src));
           //Get all pages from document and store them in a list
           List<PDPage> pages=doc.getDocumentCatalog().getAllPages();
           //create iterator object so it is easy to access each page from the list
           Iterator<PDPage> i= pages.iterator();
           int count=1; //count variable used to separate each image file
           //Convert every page of the pdf document to a unique image file
           System.out.println("Please wait...");
           while(i.hasNext()){
            PDPage page=i.next(); 
            BufferedImage bi=page.convertToImage();
            ImageIO.write(bi, "jpg", new File(FolderPath+"\\Page"+count+".jpg"));
            count++;
            }
           System.out.println("Conversion complete");
           }catch(IOException ie){ie.printStackTrace();}
          }

OP明确表示他需要一个“Google App Engine”(GAE)的解决方案。当前的PDFBox版本因使用AWT类而在GAE环境中无法正常工作,这一点是众所周知的。 - mkl

0

我认为icepdf可能有你想要的东西。

我曾经使用这个开源项目将上传的PDF文件转换成图片,以便在在线目录中使用。

import org.icepdf.core.exceptions.PDFException;
import org.icepdf.core.exceptions.PDFSecurityException;
import org.icepdf.core.pobjects.Document;
import org.icepdf.core.pobjects.Page;
import org.icepdf.core.util.GraphicsRenderingHints;


public byte[][] convert(byte[] pdf, String format) {

    Document document = new Document();
    try {
        document.setByteArray(pdf, 0, pdf.length, null);

    } catch (PDFException ex) {
        System.out.println("Error parsing PDF document " + ex);
    } catch (PDFSecurityException ex) {
        System.out.println("Error encryption not supported " + ex);
    } catch (FileNotFoundException ex) {
        System.out.println("Error file not found " + ex);
    } catch (IOException ex) {
        System.out.println("Error handling PDF document " + ex);
    }
    byte[][] imageArray = new byte[document.getNumberOfPages()][];
    // save page captures to bytearray.
    float scale = 1.75f;
    float rotation = 0f;

    // Paint each pages content to an image and write the image to file
    for (int i = 0; i < document.getNumberOfPages(); i++) {
        BufferedImage image = (BufferedImage)
                document.getPageImage(i,
                                      GraphicsRenderingHints.SCREEN,
                                      Page.BOUNDARY_CROPBOX, rotation, scale);
       try {
            //get the picture util object
            PictureUtilLocal pum = (PictureUtilLocal) Component
            .getInstance("pictureUtil");
            //load image into util
            pum.loadBuffered(image);

            //write image in desired format
            imageArray[i] = pum.imageToByteArray(format, 1f);

            System.out.println("\t capturing page " + i);

        } catch (IOException e) {
            e.printStackTrace();
        }
        image.flush();
    }
    // clean up resources
    document.dispose();
    return imageArray;
}

需要注意的是,我在使用这个库时遇到了在open-jdk上抛出SegFault的问题,在Sun的上运行良好。不确定在GAE上会发生什么。我记不清楚哪个版本有问题了,所以请注意。


我不知道为什么他们要踩它,但是在过去的四年里,我一直在生产环境中运行它,没有任何问题。 - natedennis
出于好奇,你使用过pdf-renderer吗?我在使用Apache PdfBox将PDF的单个页面转换为PNG时遇到了问题,但是pdf-renderer似乎解决了这个问题类似于此帖子。我很少听到人谈论它,所以担心我可能错过了一些问题/缺点。 - Don Cheadle
我没有。我不知道这个..实际上,我在2010年写了上面这段代码的第一个版本。pdf-renderer直到一年后才开始。这可能是一个很好的项目去尝试。我是一个程序员..我总是对更好的方法感兴趣。“Pdf-renderer是Swinglabs的一个子项目,于2011年1月开始,有571名成员。项目管理员是rbair、tomoke、joshy和Jan Haderka。” - natedennis
那么,我猜你对它没有任何意见? - Don Cheadle

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接