В моем приложении я пытаюсь создать файл PDF с использованием FTL (файл шаблона бесплатного создателя). Я не вижу ошибок после выполнения приведенного ниже кода, но PDF создается с пустыми данными и без фоновых изображений. Пробовал несколько раз с фактическими данными, но я столкнулся с той же проблемой
// Передача данных в шаблон (FTL): datamodel содержит данные, добавленные в шаблон динамически. publi c byte [] genPdf (PDFDataTo dataModel) выдает исключение {System.out.println (pdfDataTo);
Template template = null;
ByteArrayOutputStream bos = null;
// Write the binary bytes to the byte array output stream.
Writer out = null;
try {
template = getTemplate(dataModel); //return the template from config
System.out.println(template); //template if printed wihout data
bos = new ByteArrayOutputStream();
out = new OutputStreamWriter(bos);
template.process(dataModel, out);
System.out.println(out);
return convertFtlTOPdf(bos);
} catch (Exception e) {
throw new PdfSystemException(e);
} finally {
try {
if(out!=null)
out.close();
if(bos!=null)
bos.close();
} catch (IOException ioe) {
throw new PdfSystemException(ioe);
}
}
}
//Return the Freemaker Template
private Template getTemplate(PDFDataTo pdfDataTO) throws IOException {
Configuration config = PDFConfigurationSingleton.getFreeMarkerConfiguration(pdfDataTO.getTemplatePath(), pdfDataTO.getTemplateName());
return config.getTemplate(pdfDataTO.getTemplateName());
}
//Convert Template data to PDF
private byte[] convertFtlTOPdf(ByteArrayOutputStream fbos) throws PdfSystemException {
System.out.println("fbos.size()"+fbos.size());
ByteArrayOutputStream pbos = new ByteArrayOutputStream();
try {
DocumentBuilderFactoryImpl factory = new DocumentBuilderFactoryImpl();
// DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setIgnoringComments(true);
factory.setValidating(false);
factory.setNamespaceAware(true);
factory.setFeature("http://xml.org/sax/features/namespaces", false);
factory.setFeature("http://xml.org/sax/features/validation", false);
factory.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
long start1 = System.currentTimeMillis();
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setEntityResolver(new EntityResolver() { public InputSource
resolveEntity(String publicId, String systemId) throws SAXException,
IOException { if (systemId.contains("foo.dtd")) { return new InputSource(new
StringReader("")); } else { return null; } } });
Document doc = builder.parse(new ByteArrayInputStream(fbos.toByteArray()));
long start = System.currentTimeMillis();
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(doc, null);
renderer.layout();
renderer.createPDF(pbos);
return pbos.toByteArray();
} catch (Exception e) {
throw new PdfSystemException(e);
} finally {
try {
pbos.close();
} catch (IOException e) {
throw new PdfSystemException(e);
}
}
}