我正在使用以blob形式存储在数据库中的png图像,并且我还使用PDFBox 2.0.13
来完成此任务。
我用了
LosslessFactory.createFromImage ( BufferedImage) // null pointer exception raised
我不知道是否应该使用:
PDImageXObject.createFromFile
因为它适用于png图像类型..有什么方法可以使用它并从blob创建文件对象以与PDImageXObject.createFromFile
一起使用?
如果还有其他方法可以将blob png图像添加到pdf
文件中..那将是一个很大的帮助。
我正在使用这种方法
public byte[] getArchiveDocument(String id, String index, String type) throws Exception {
String idx = getIDForDocument(index, id, type);
System.out.println(idx);
String s=" ";
String z="";
SystemDbManager sx=new SystemDbManager();
sx.executeStatment(s);
Blob blob = null;
ResultSet rs=null;
PDDocument document = new PDDocument();
try {
rs=sx.getResultSet(z);
if (rs.next()) {
blob=rs.getBlob("imageole");
InputStream in = blob.getBinaryStream();
PDPage page = new PDPage();
document.addPage(page);
File f = File.createTempFile("stream2file", ".tiff");
f.deleteOnExit();
try (FileOutputStream out = new FileOutputStream(f)) {
IOUtils.copy(in, out);
}
PDImageXObject img = PDImageXObject.createFromFileByContent(f, document);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.drawImage(img, 0, 0);
contentStream.close();
in.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
rs.close();
sx.close();
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
document.save(byteArrayOutputStream);
document.close();
return byteArrayOutputStream.toByteArray();
}
错误堆栈
java.io.IOException: First image in tiff is not a single tile/strip
at org.apache.pdfbox.pdmodel.graphics.image.CCITTFactory.extractFromTiff(CCITTFactory.java:435)
at org.apache.pdfbox.pdmodel.graphics.image.CCITTFactory.createFromRandomAccessImpl(CCITTFactory.java:203)
at org.apache.pdfbox.pdmodel.graphics.image.CCITTFactory.createFromFile(CCITTFactory.java:164)
at org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject.createFromFileByContent(PDImageXObject.java:257)
at ps.gov.gpc.tools.ArchiveModel.getArchiveDocument(ArchiveModel.java:117)
at ps.gov.gpc.siteService.action.ShowArchiveDocAction.execute(ShowArchiveDocAction.java:48)
at org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
at org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.displaytag.filter.ResponseOverrideFilter.doFilter(ResponseOverrideFilter.java:125)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
谢谢。
答案 0 :(得分:0)
您是否尝试过使用doc中提到的PDImageXObject.createFromByteArray
?下面的代码对我来说很好用(JDK 8):
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
public class PDFBox {
public static void main(String[] args) {
String path = "/home/yohanesgultom/Documents/example.pdf";
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
try {
// convert image to byte array
// for Blob use java.sql.Blob.getBytes(long pos, int length)
BufferedImage image = ImageIO.read(new File("/home/yohanesgultom/Pictures/php7.png"));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(image, "png", outputStream);
byte[] imageByte = outputStream.toByteArray();
// convert byte array to image object
PDImageXObject imageObject = PDImageXObject.createFromByteArray(doc, imageByte, null);
PDPageContentStream contentStream = new PDPageContentStream(doc, page);
// draw image on top of page
contentStream.drawImage(imageObject, 25, 600);
contentStream.close();
doc.addPage(page);
doc.save(path);
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
在here中有一个很好的示例,将java.sql.Blob
转换为byte[]
。