在Java GAE上将PDF页面转换为JPG
我正在寻找一个开源java库,它使我能够在服务器端将PDF的单个页面呈现为JPG或PNG 不幸的是,它不能使用任何其他在Java GAE上将PDF页面转换为JPG,java,image,google-app-engine,pdf,Java,Image,Google App Engine,Pdf,我正在寻找一个开源java库,它使我能够在服务器端将PDF的单个页面呈现为JPG或PNG 不幸的是,它不能使用任何其他java.awt.*类 java.awt.datatransfer.DataFlavor java.awt.datatransfer.MimeType java.awt.datatransfer.Transferable 如果有什么办法的话,一个小小的代码片段就太棒了。我相信可能有你想要的东西 我曾经使用过这个开源项目,将上传的PDF转换成图像,用于在线目录 import o
java.awt.*
类
java.awt.datatransfer.DataFlavor
java.awt.datatransfer.MimeType
java.awt.datatransfer.Transferable
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;
}
import org.icepdf.core.exceptions.pdfeexception;
导入org.icepdf.core.exceptions.PDFSecurityException;
导入org.icepdf.core.pobjects.Document;
导入org.icepdf.core.pobjects.Page;
导入org.icepdf.core.util.graphicsrenderingints;
公共字节[]转换(字节[]pdf,字符串格式){
文档=新文档();
试一试{
setByteArray(pdf,0,pdf.length,null);
}捕获(PDFEException ex){
System.out.println(“解析PDF文档时出错”+ex);
}捕获(PDFSecurityException例外){
System.out.println(“不支持错误加密”+ex);
}捕获(FileNotFoundException ex){
System.out.println(“未找到错误文件”+ex);
}捕获(IOEX异常){
System.out.println(“错误处理PDF文档”+ex);
}
字节[][]图像数组=新字节[document.getNumberOfPages()][];
//将页面捕获保存到bytearray。
浮子刻度=1.75f;
浮动旋转=0f;
//将每个页面内容绘制为图像,并将图像写入文件
对于(int i=0;i
不过,请注意,这个库在OpenJDK上抛出一个SegFault时,我遇到了麻烦。在孙家工作得很好。不知道它会在GAE上做什么。我不记得是哪个版本出现了问题,所以请注意。您可以使用apache实现此目的,并使用以下代码将两个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();}
}
public void convertPDFToJPG(字符串src,字符串FolderPath){
试一试{
File folder1=新文件(FolderPath+“\\”;
comparePDF cmp=新的comparePDF();
cmp.rmdir(folder1);
//在文档对象中加载pdf文件
PDDocument doc=PDDocument.load(新文件输入流(src));
//从文档中获取所有页面并将其存储在列表中
列表页面=doc.getDocumentCatalog().getAllPages();
//创建迭代器对象,以便轻松访问列表中的每个页面
迭代器i=pages.Iterator();
int count=1;//用于分隔每个图像文件的count变量
//将pdf文档的每一页转换为唯一的图像文件
System.out.println(“请稍候…”);
while(i.hasNext()){
PDPage page=i.next();
BuffereImage bi=page.convertToImage();
写入(bi,“jpg”,新文件(FolderPath+“\\Page”+count+“.jpg”);
计数++;
}
系统输出打印项次(“转换完成”);
}catch(IOIE){ie.printStackTrace();}
}
展示了如何使用Google conversions api进行转换。但有一个问题。该api将于11月删除。也许你可以问谷歌关于任何替代方案的提示。是的,我已经看到了。但是就像你写的,这种支持很快就会停止。否则就太完美了。我将尝试从谷歌获取一些信息。嗨,你找到了其他可以进行相同转换的东西吗?我也在寻找类似的功能。我知道我可以使用谷歌硬盘从小于25Mb的pdf文件中请求图像。但我需要它来处理更大的文件。没有线索。但是,尽管他们投票反对,我在过去4年里一直在生产中运行它。没问题。出于好奇,你用过pdf渲染器吗?我在使用ApachePDFBox将一页PDF转换为PNG时遇到问题,但PDF呈现程序似乎解决了这个问题。我没有听到过太多关于它的讨论,所以我担心我遗漏了一些问题/缺点。我没有。我没有意识到。。实际上,我在2010年编写了上述代码的第一次修订版。pdf渲染器直到一年后才启动。可能是一个很好的项目。我是一名程序员。。我总是对更好的方式感兴趣。“Pdf renderer是Swinglabs的一个子项目,于2011年1月启动,拥有571名成员。项目管理员是rbair、tomoke、joshy和Jan Haderka。“嘿,我想你对这两种方式都没有意见吗?OP明确表示他需要“谷歌应用程序引擎”(GAE)的解决方案。”。当前的PDFBox版本以不在GAE环境中工作而闻名,因为它们使用的是不存在的AWT类。