gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 08:04:01 25 4
gpt4 key购买 nike

我正在寻找一个开源 java 库,它使我能够在服务器端将单页 PDF 呈现为 JPG 或 PNG。

不幸的是,它不能使用任何其他 java.awt.*

  • java.awt.datatransfer.DataFlavor
  • java.awt.datatransfer.MimeType
  • java.awt.datatransfer.Transferable

如果有任何方法,一个小代码片段会很棒。

最佳答案

我相信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 上做什么。我不记得是哪个版本有问题,所以请注意。

关于java - 在 Java-GAE 上将 PDF 页面转换为 JPG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12221026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com