Tag Archives: Document processing tools

IText The document has no pages [How to Solve]

Problem Description:

com.itextpdf.text.ExceptionConverter: The document has no pages.
Caused by: java.io.IOException: The document has no pages.

Problem analysis:

1. When using the template to export PDF file, the template is not read to generate content because there is no data.

public void getTemplatePDF(HttpServletResponse response) {
        try {
            // Template Path
            String templatePath = "doc/Template.pdf";
            ServletOutputStream os = null;
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            PdfStamper stamper = null;
            PdfReader reader = null;// Read pdf Template 
            Document doc = null;
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition", "attachment;fileName=测试.pdf");
            os = response.getOutputStream();// Output Stream
            doc = new Document();
            PdfCopy copy = new PdfCopy(doc, os);
            doc.open();
            List<String> resultList = new ArrayList<>();
            for (int i = 0; i < resultList.size(); i++) {
                reader = new PdfReader(templatePath);
                bos = new ByteArrayOutputStream();
                stamper = new PdfStamper(reader, bos);
                AcroFields form = stamper.getAcroFields();
                // Add Chinese fonts to the form. The system font is used here. If not set, Chinese may not be displayed (use the font in iTextAsian.jar)
                BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
                form.addSubstitutionFont(bf);
                // If false then the generated PDF file can also be edited, must be set to true
                stamper.setFormFlattening(true);
                stamper.close();
                reader.close();
                PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
                copy.addPage(importPage);
            }
            doc.close();
            try {
                os.flush();
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                bos.flush();
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Solution: generate default content when there is no data.

public void getTemplatePDF(HttpServletResponse response) {
        try {
            // Template Path
            String templatePath = "doc/Template.pdf";
            ServletOutputStream os = null;
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            PdfStamper stamper = null;
            PdfReader reader = null;// Read pdf Template 
            Document doc = null;
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition", "attachment;fileName=测试.pdf");
            os = response.getOutputStream();// Output Stream
            doc = new Document();
            PdfCopy copy = new PdfCopy(doc, os);
            doc.open();
            List<String> resultList = new ArrayList<>();
            for (int i = 0; i < resultList.size(); i++) {
                reader = new PdfReader(templatePath);
                bos = new ByteArrayOutputStream();
                stamper = new PdfStamper(reader, bos);
                AcroFields form = stamper.getAcroFields();
                // Add Chinese fonts to the form. The system font is used here. If you don't set it, Chinese may not be displayed (use the font in iTextAsian.jar)
                BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
                form.addSubstitutionFont(bf);
                // if false then the generated PDF file can also be edited, must be set to true
                stamper.setFormFlattening(true);
                stamper.close();
                reader.close();
                PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
                copy.addPage(importPage);
            }
            // no data case
            if (resultList == null || resultList.size() <= 0) {
                reader = new PdfReader(templatePath);
                bos = new ByteArrayOutputStream();
                stamper = new PdfStamper(reader, bos);
                AcroFields form = stamper.getAcroFields();
                // add Chinese fonts to the form here using the system font. Not set, Chinese may not be able to display (using iTextAsian.jar in the font)
                BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
                form.addSubstitutionFont(bf);
                // If false then the generated PDF file can also be edited, must be set to true
                stamper.setFormFlattening(true);
                stamper.close();
                reader.close();
                PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
                copy.addPage(importPage);
            }
            doc.close();
            try {
                os.flush();
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                bos.flush();
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }