Skip to content

issue occurs randomly: The page 2 was requested but the document has only 1 pages #164

Description

@TonyLuo

I work with openPDF using flying-saucer-pdf-openpdf to generate pdf, following issue occurs randomly . the input data is the same, but sometimes it works well, sometimes it goes wrong, no idea what happened.

2019-03-25 01:11:58,191 ERROR [sanhao-Executor-1] SimpleAsyncUncaughtExceptionHandler: Unexpected error occurred invoking async method: public void com.zykj.sanhao.service.PdfService.generatePdfAndSendMail(com.zykj.sanhao.business.work_order.dto.WorkOrderDTO,boolean)
java.lang.RuntimeException: The page 2 was requested but the document has only 1 pages.
        at com.lowagie.text.pdf.PdfWriter.close(PdfWriter.java:1153)
        at com.lowagie.text.pdf.PdfDocument.close(PdfDocument.java:813)
        at com.lowagie.text.Document.close(Document.java:451)
        at org.xhtmlrenderer.pdf.ITextRenderer.createPDF(ITextRenderer.java:337)
        at org.xhtmlrenderer.pdf.ITextRenderer.createPDF(ITextRenderer.java:265)
        at com.zykj.sanhao.service.PdfService.generatePdf(PdfService.java:401)
        at com.zykj.sanhao.service.PdfService.generatePdfAndSendMail(PdfService.java:116)
        at com.zykj.sanhao.service.PdfService$$FastClassBySpringCGLIB$$3b40b191.invoke(<generated>)
        at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
        at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
        at org.springframework.aop.interceptor.AsyncExecutionInterceptor.lambda$invoke$0(AsyncExecutionInterceptor.java:115)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at io.github.jhipster.async.ExceptionHandlingAsyncTaskExecutor.lambda$createWrappedRunnable$1(ExceptionHandlingAsyncTaskExecutor.java:68)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
2019-03-25 01:12:00,825 INFO  [hz.sanhao.cached.thread-4] TcpIpConnectionManager: [172.16.0.3]:5701 [dev] [3.9.4] Established socket connection between /172.16.0.3:41156 and /129.204.175.132:5701
2019-03-25 01:12:00,826 INFO  [hz.sanhao.IO.thread-Acceptor] TcpIpAcceptor: [172.16.0.3]:5701 [dev] [3.9.4] Accepting socket connection from /129.204.175.132:41156
2019-03-25 01:12:00,826 INFO  [hz.sanhao.cached.thread-4] TcpIpConnectionManager: [172.16.0.3]:5701 [dev] [3.9.4] Established socket connection between /172.16.0.3:5701 and /129.204.175.132:41156

private String generatePdf(String reportTemplateName, Context context, String filePath) throws IOException {
        String reportContent = templateService.processThymeleafTemplateIntoString(reportTemplateName, context);



        renderer.setDocumentFromString(reportContent);
        renderer.layout();
        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(filePath);
            renderer.createPDF(outputStream);


        } catch (IOException e) {
            log.error("[generatePdf]", e);
        } catch (DocumentException e) {
            log.error("[generatePdf]", e);
        } finally {
            if (null != outputStream) {
                outputStream.close();
            }
        }

        return filePath;
    }
 <dependency>
            <groupId>org.xhtmlrenderer</groupId>
            <artifactId>flying-saucer-pdf-openpdf</artifactId>
            <version>9.1.16</version>
            <exclusions>
                    <exclusion>
                        <!--Since openpdf 1.2.0 loading Image with path/URL is not working , 1.2.9 fixed this issue-->
                        <!--https://github.com/LibrePDF/OpenPDF/issues/133-->
                        <groupId>com.github.librepdf</groupId>
                        <artifactId>openpdf</artifactId>
                    </exclusion>
            </exclusions>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.github.librepdf/openpdf -->
        <dependency>
            <groupId>com.github.librepdf</groupId>
            <artifactId>openpdf</artifactId>
            <version>1.2.10</version>
        </dependency>

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions