使用Apache POI将.docx转换为pdf得到OutOfMemory异常

时间:2017-11-19 17:05:48

标签: java itext apache-poi xwpf

我正在尝试使用Apache POI将.docx文件转换为pdf。我在StackOverflow中读到了几个关于它的问题,但我没有看到有人遇到我的问题。

我正在使用此方法将Ms doc转换为pdf:

max1

这是我的pom.xml的相关部分:

private void thumbForDoc(File file) {
        String fullPath = FilenameUtils.getFullPath(file.getAbsolutePath());
        String fileName = FilenameUtils.getBaseName(file.getAbsolutePath());
        try {
            InputStream doc = new FileInputStream(file);
            XWPFDocument document = new XWPFDocument(doc);
            PdfOptions options = PdfOptions.create();

            File destFile = new File(fullPath + fileName + ".pdf");
            OutputStream out = new FileOutputStream(destFile);
            PdfConverter.getInstance().convert(document, out, options);

        } catch (Exception e) {
            log.warn(String.format("Error trying to create the thumbnail for the file %s", file.getAbsolutePath()), e);
        }
    }

不幸的是转换没有发生,过了一段时间,我有一个OutOfMemory异常:

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.15</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.15</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.15</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.15</version>
            <type>jar</type>
        </dependency>

        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>org.apache.poi.xwpf.converter.core</artifactId>
            <version>1.0.6</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
            <version>1.0.6</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.xdocreport.itext.extension</artifactId>
            <version>2.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.12</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>ooxml-schemas</artifactId>
            <version>1.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.xmlbeans</groupId>
            <artifactId>xmlbeans</artifactId>
            <version>2.6.0</version>
        </dependency>

我很困惑。事实上,我将Apache POI降级为3.15,因为3.17似乎与xwpf不兼容。 此外,最终结果改变了库的更改版本。在某些情况下,我能够生成PDF,但它已被破坏,大小超过400MB !!

有些建议值得赞赏。

=============================================== =====

这些是我的依赖项:

java.lang.OutOfMemoryError: null
    at java.io.ByteArrayOutputStream.hugeCapacity(ByteArrayOutputStream.java:123) ~[?:1.8.0_131]
    at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:117) ~[?:1.8.0_131]
    at java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93) ~[?:1.8.0_131]
    at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:153) ~[?:1.8.0_131]
    at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82) ~[?:1.8.0_131]
    at java.io.BufferedOutputStream.write(BufferedOutputStream.java:95) ~[?:1.8.0_131]
    at com.lowagie.text.pdf.OutputStreamCounter.write(Unknown Source) ~[itext-2.1.7.js6.jar:?]
    at com.lowagie.text.pdf.PdfDictionary.toPdf(Unknown Source) ~[itext-2.1.7.js6.jar:?]
    at com.lowagie.text.pdf.PdfDictionary.toPdf(Unknown Source) ~[itext-2.1.7.js6.jar:?]
    at com.lowagie.text.pdf.PdfDictionary.toPdf(Unknown Source) ~[itext-2.1.7.js6.jar:?]
    at com.lowagie.text.pdf.PdfIndirectObject.writeTo(Unknown Source) ~[itext-2.1.7.js6.jar:?]
    at com.lowagie.text.pdf.PdfWriter$PdfBody.add(Unknown Source) ~[itext-2.1.7.js6.jar:?]
    at com.lowagie.text.pdf.PdfWriter$PdfBody.add(Unknown Source) ~[itext-2.1.7.js6.jar:?]
    at com.lowagie.text.pdf.PdfWriter$PdfBody.add(Unknown Source) ~[itext-2.1.7.js6.jar:?]
    at com.lowagie.text.pdf.PdfWriter.addToBody(Unknown Source) ~[itext-2.1.7.js6.jar:?]
    at com.lowagie.text.pdf.PdfPages.addPage(Unknown Source) ~[itext-2.1.7.js6.jar:?]
    at com.lowagie.text.pdf.PdfWriter.add(Unknown Source) ~[itext-2.1.7.js6.jar:?]
    at com.lowagie.text.pdf.PdfDocument.newPage(Unknown Source) ~[itext-2.1.7.js6.jar:?]
    at com.lowagie.text.pdf.PdfDocument.addPTable(Unknown Source) ~[itext-2.1.7.js6.jar:?]
    at com.lowagie.text.pdf.PdfDocument.add(Unknown Source) ~[itext-2.1.7.js6.jar:?]
    at com.lowagie.text.Document.add(Unknown Source) ~[itext-2.1.7.js6.jar:?]
    at fr.opensagres.xdocreport.itext.extension.ExtendedDocument.add(ExtendedDocument.java:114) ~[fr.opensagres.xdocreport.itext.extension-2.0.1.jar:2.0.1]
    at org.apache.poi.xwpf.converter.pdf.internal.elements.StylableDocument.flushTable(StylableDocument.java:345) ~[org.apache.poi.xwpf.converter.pdf-1.0.6.jar:1.0.6]
    at org.apache.poi.xwpf.converter.pdf.internal.elements.StylableDocument.pageBreak(StylableDocument.java:128) ~[org.apache.poi.xwpf.converter.pdf-1.0.6.jar:1.0.6]
    at org.apache.poi.xwpf.converter.pdf.internal.elements.StylableDocument.columnBreak(StylableDocument.java:107) ~[org.apache.poi.xwpf.converter.pdf-1.0.6.jar:1.0.6]
    at org.apache.poi.xwpf.converter.pdf.internal.elements.StylableDocument.simulateText(StylableDocument.java:211) ~[org.apache.poi.xwpf.converter.pdf-1.0.6.jar:1.0.6]
    at org.apache.poi.xwpf.converter.pdf.internal.elements.StylableDocument.addElement(StylableDocument.java:94) ~[org.apache.poi.xwpf.converter.pdf-1.0.6.jar:1.0.6]
    at org.apache.poi.xwpf.converter.pdf.internal.PdfMapper.endVisitParagraph(PdfMapper.java:409) ~[org.apache.poi.xwpf.converter.pdf-1.0.6.jar:1.0.6]
    at org.apache.poi.xwpf.converter.pdf.internal.PdfMapper.endVisitParagraph(PdfMapper.java:111) ~[org.apache.poi.xwpf.converter.pdf-1.0.6.jar:1.0.6]
    at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.visitParagraph(XWPFDocumentVisitor.java:333) ~[org.apache.poi.xwpf.converter.core-1.0.6.jar:1.0.6]
    at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.visitBodyElements(XWPFDocumentVisitor.java:247) ~[org.apache.poi.xwpf.converter.core-1.0.6.jar:1.0.6]
    at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.start(XWPFDocumentVisitor.java:199) ~[org.apache.poi.xwpf.converter.core-1.0.6.jar:1.0.6]
    at org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:56) ~[org.apache.poi.xwpf.converter.pdf-1.0.6.jar:1.0.6]
    at org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:38) ~[org.apache.poi.xwpf.converter.pdf-1.0.6.jar:1.0.6]
    at org.apache.poi.xwpf.converter.core.AbstractXWPFConverter.convert(AbstractXWPFConverter.java:45) ~[org.apache.poi.xwpf.converter.core-1.0.6.jar:1.0.6]
    at server.storage.ThumbnailGenerator.thumbForDoc(ThumbnailGenerator.java:158) ~[classes/:?]
    at server.storage.ThumbnailGenerator.createThumbnail(ThumbnailGenerator.java:81) ~[classes/:?]
    at server.storage.ThumbnailGenerator$$FastClassBySpringCGLIB$$6fd76f41.invoke(<generated>) ~[classes/:?]
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738) ~[spring-aop-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.aop.interceptor.AsyncExecutionInterceptor$1.call(AsyncExecutionInterceptor.java:115) [spring-aop-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.security.concurrent.DelegatingSecurityContextCallable.call(DelegatingSecurityContextCallable.java:86) [spring-security-core-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_131]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) [?:1.8.0_131]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [?:1.8.0_131]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [?:1.8.0_131]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [?:1.8.0_131]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_131]

0 个答案:

没有答案