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();
}
}
Read More:
- [Solved] Error: The superclass, ‘Animal‘, has no unnamed constructor that takes no arguments.
- [Solved] volatile was removed and now has no effect. Use `with torch.no_grad():` instead.
- [Jeston Xavier NX Compile librealsense Error] AttributeError: module ‘pyrealsense2‘ has no attribute ‘pipeline‘
- Numpy.exp Function Error ‘Float’ object has no attribute ‘exp’
- Pyqt5 Error: AttributeError: ‘QWidget‘ object has no attribute ‘setCentralWidget‘
- [Solved] Module ‘scipy.misc‘ has no attribute ‘imread‘
- module ‘networkx’ has no attribute ‘from_pandas_dataframe’
- [Solved] redis Error: AttributeError: ‘list‘ object has no attribute ‘decode‘
- Laravel model save if the table has no ID field Error [How to Solve]
- [Solved] AttributeError : ‘GridSearchCV‘ object has no attribute ‘grid_scores_‘
- Pytorch error: `module ‘torch‘ has no attribute ‘__version___‘`
- [Solved] AttributeError: ‘depthai.node.ObjectTracker‘ object has no attribute ‘setTrackerIdAssigmentPolicy‘
- [Solved] AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘
- [Solved] OpenCV Error: AttributeError: module ‘cv2‘ has no attribute ‘data‘
- PySpark error: AttributeError: ‘NoneType‘ object has no attribute ‘_jvm‘
- [Solved] IDEA Git Update Error: Master has no tracked branch
- [How to Solve] Tesorflow: module ‘pandas.core.computation’ has no attribute ‘expressions’
- [Solved] AttributeError: module ‘keras.preprocessing.image‘ has no attribute ‘load_img‘
- Peewee insert data error:’buffer’ object has no attribute’translate’
- [Solved] This application has no explicit mapping for /error, so you are seeing this as a fallback