将 docx 文件转换为 pdf

时间:2021-08-01 10:19:58

标签: itext apache-poi

我正在使用 apache poi 创建 Word 文档。在我的项目结束时,我想将 word 文档转换为 pdf 文件。为此,我正在使用 itext 库,但我收到了这样的 nullpointerexception。

13:11:30,144 SEVERE [javax.enterprise.resource.webcontainer.jsf.context] (default task-6) fr.opensagres.poi.xwpf.converter.core.XWPFConverterException: java.lang.NullPointerException
    at fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:71)
    at fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:39)
    at fr.opensagres.poi.xwpf.converter.core.AbstractXWPFConverter.convert(AbstractXWPFConverter.java:46)
    at com.utc.pw.ui.TestWSV2500View.createTestWS(TestWSV2500View.java:6273)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:181)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:289)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
    at org.jboss.weld.module.web.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
    at org.jboss.weld.module.web.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
    at org.jboss.weld.module.web.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
    at org.jboss.weld.module.web.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:126)
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:72)
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:490)
    at javax.faces.component.UICommand.broadcast(UICommand.java:211)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:847)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1395)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:58)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:76)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:177)
    at javax.faces.webapp.FacesServlet.executeLifecyle(FacesServlet.java:707)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:451)
    at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)
    at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:89)
    at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
    at com.utc.pw.filter.UserFilter.doFilter(UserFilter.java:52)
    at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
    at org.omnifaces.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:124)
    at org.omnifaces.filter.HttpFilter.doFilter(HttpFilter.java:108)
    at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
    at io.opentracing.contrib.jaxrs2.server.SpanFinishingFilter.doFilter(SpanFinishingFilter.java:55)
    at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
    at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
    at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
    at io.undertow.servlet.handlers.ServletChain$1.handleRequest(ServletChain.java:68)
    at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
    at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:132)
    at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
    at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
    at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
    at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
    at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
    at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at org.wildfly.extension.undertow.deployment.GlobalRequestControllerHandler.handleRequest(GlobalRequestControllerHandler.java:68)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
    at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
    at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
    at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
    at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
    at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
    at org.wildfly.extension.undertow.security.SecurityContextThreadSetupAction.lambda$create$0(SecurityContextThreadSetupAction.java:105)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502)
    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
    at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
    at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
    at io.undertow.server.Connectors.executeRootHandler(Connectors.java:364)
    at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:830)
    at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
    at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1982)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
    at fr.opensagres.poi.xwpf.converter.core.utils.XWPFTableUtil.getGridColList(XWPFTableUtil.java:184)
    at fr.opensagres.poi.xwpf.converter.core.utils.XWPFTableUtil.computeColWidths(XWPFTableUtil.java:117)
    at fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.visitTable(XWPFDocumentVisitor.java:898)
    at fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.visitBodyElements(XWPFDocumentVisitor.java:235)
    at fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.start(XWPFDocumentVisitor.java:183)
    at fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:57)
    ... 82 more

我正在使用这个库

  1. fr.opensagres.poi.xwpf.converter.core-2.0.1.jar
  2. fr.opensagres.poi.xwpf.converter.pdf-2.0.1.jar
  3. fr.opensagres.xdocreport.itext.extension-2.0.1.jar
  4. itextpdf-5.5.0.jar
  5. poi-3.16
  6. ooxml-schemas-1.4.jar

我的创建代码也是这样

new File(System.getProperty("jboss.server.data.dir"), "corrolog/TestWS/" ).mkdir();
        FileOutputStream out = new FileOutputStream(System.getProperty("jboss.server.data.dir")+"/corrolog/TestWS/"+strSO+" Test Workscope "+revision+".docx");  
        

        document.write(out);
        PdfOptions options=null;
        OutputStream outPDF = new FileOutputStream(new File(System.getProperty("jboss.server.data.dir")+"/corrolog/TestWS/"+strSO+" Test Workscope "+revision+".pdf"));
        PdfConverter.getInstance().convert(document, outPDF, options);
        

        document.close();
        out.close();

我不明白为什么它会给我空指针异常。 word文档创建成功,但是pdf文件出现nullpointerexception。

更新

word

pdf

我的表创建代码

//Table1
        XWPFTable table1=document.createTable();
        table1.getCTTbl().addNewTblPr().addNewTblW().setW(BigInteger.valueOf(10750));
        table1.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(3100));
        table1.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(7650));
        table1.getCTTbl().getTblPr().getTblBorders().getRight().setSz(BigInteger.valueOf(12));
        table1.getCTTbl().getTblPr().getTblBorders().getTop().setSz(BigInteger.valueOf(12));
        table1.getCTTbl().getTblPr().getTblBorders().getLeft().setSz(BigInteger.valueOf(12));
        table1.getCTTbl().getTblPr().getTblBorders().getBottom().setSz(BigInteger.valueOf(12));
        table1.getCTTbl().getTblPr().getTblBorders().getBottom().setColor("00b0f0");
        table1.getCTTbl().getTblPr().getTblBorders().getRight().setColor("00b0f0");
        table1.getCTTbl().getTblPr().getTblBorders().getLeft().setColor("00b0f0");
        table1.getCTTbl().getTblPr().getTblBorders().getTop().setColor("00b0f0");
        table1.getCTTbl().getTblPr().getTblBorders().getTop().setVal(STBorder.THICK);
        table1.getCTTbl().getTblPr().getTblBorders().getBottom().setVal(STBorder.THICK);
        table1.getCTTbl().getTblPr().getTblBorders().getRight().setVal(STBorder.THICK);
        table1.getCTTbl().getTblPr().getTblBorders().getLeft().setVal(STBorder.THICK);
        table1.setInsideHBorder(XWPFBorderType.THICK, 12, 0, "00b0f0");
        table1.setInsideVBorder(XWPFBorderType.THICK, 12, 0, "00b0f0");
        XWPFTableRow row0=table1.getRow(0);
        XWPFTableRow row1=table1.createRow();
        //Row0_Cell0
        XWPFTableCell cell0=row0.getCell(0);
        CTTblWidth width_cell0 = cell0.getCTTc().addNewTcPr().addNewTcW();
        width_cell0.setType(STTblWidth.DXA);
        width_cell0.setW(BigInteger.valueOf(3100));
        cell0.removeParagraph(0);
        cell0.getCTTc().addNewTcPr().addNewShd().setFill("daeef3");
        XWPFParagraph paragraph_cell_0=cell0.addParagraph();
        paragraph_cell_0.setSpacingAfter(0);
        paragraph_cell_0.setVerticalAlignment(TextAlignment.CENTER);
        paragraph_cell_0.setAlignment(ParagraphAlignment.CENTER);
        cell0.setVerticalAlignment(XWPFVertAlign.CENTER);
        XWPFRun run_cell_0=paragraph_cell_0.createRun();
        FileInputStream tec=new FileInputStream("C:\\Users\\ftk1187\\Desktop\\testWS-TEC.png");
        run_cell_0.addPicture(tec, XWPFDocument.PICTURE_TYPE_PNG, "tec", Units.toEMU(54), Units.toEMU(31));
        run_cell_0.addBreak();
        run_cell_0.setText("TEC / ");
        run_cell_0.setFontFamily("Arial");
        run_cell_0.setFontSize(7);
        run_cell_0.setBold(true);
        XWPFRun run_cell0_1=paragraph_cell_0.createRun();
        run_cell0_1.setText("TURKISH ENGINE CENTER");
        run_cell0_1.setFontFamily("Arial");
        run_cell0_1.setFontSize(7);
        run_cell0_1.setItalic(true);
        run_cell0_1.setBold(true);
        mergeCellVertically(table1, 0, 0, 1);
        tec.close();
        //Row0_Cell1
        row0.addNewTableCell();
        XWPFTableCell cell1=row0.getCell(1);
        cell1.getCTTc().addNewTcPr().addNewShd().setFill("daeef3");
        cell1.removeParagraph(0);
        CTTblWidth width_cell1= cell1.getCTTc().addNewTcPr().addNewTcW();
        width_cell1.setType(STTblWidth.DXA);
        width_cell1.setW(BigInteger.valueOf(7650));
        XWPFParagraph paragraph_cell_1=cell1.addParagraph();
        paragraph_cell_1.setSpacingBefore(100);
        paragraph_cell_1.setSpacingAfter(75);
        paragraph_cell_1.setVerticalAlignment(TextAlignment.CENTER);
        paragraph_cell_1.setAlignment(ParagraphAlignment.CENTER);
        cell1.setVerticalAlignment(XWPFVertAlign.CENTER);
        XWPFRun run_cell1_0=paragraph_cell_1.createRun();
        run_cell1_0.setText("ENGINE TEST WORKSCOPE");
        run_cell1_0.setBold(true);
        run_cell1_0.setFontFamily("Arial");
        run_cell1_0.setFontSize(16);
        //Row1_Cell0
        XWPFTableCell cell3=row1.getCell(0);
        cell3.getCTTc().addNewTcPr().addNewShd().setFill("daeef3"); 
        //Row1_Cell1
        XWPFTableCell cell2=row1.getCell(1);
        cell2.removeParagraph(0);
        cell2.getCTTc().addNewTcPr().addNewShd().setFill("daeef3");
        XWPFParagraph paragraph_cell_2=cell2.addParagraph();
        paragraph_cell_2.setSpacingAfter(50);
        paragraph_cell_2.setVerticalAlignment(TextAlignment.CENTER);
        paragraph_cell_2.setAlignment(ParagraphAlignment.CENTER);
        cell2.setVerticalAlignment(XWPFVertAlign.CENTER);
        XWPFRun run_cell2_0=paragraph_cell_2.createRun();
        run_cell2_0.setText("MODEL "+outgoingModel+" / ESN : "+esn);
        run_cell2_0.setBold(true);
        run_cell2_0.setFontFamily("Arial");
        run_cell2_0.setFontSize(16);

1 个答案:

答案 0 :(得分:2)

您可以使用 iText7 和 pdfOffice 来转换 word 和演示文件。要查看 pdfOffice 的工作原理,请转到下一个链接:https://itextpdf.com/en/products/itext-7/convert-ms-office-to-pdf-pdfoffice