Generate a PDF
Updated: May 28, 2022
This example shows you how to read data from an XML file and write it to a PDF.
The code reads an XML file and writes its contents to a PDF. However, the input XML data can also be written to other output sources.
XML input
<?xml version="1.0" encoding="ISO-8859-1"?>
<records>
<record>
<field name="stageName ">John Wayne </field >
<field name="realName">Marion Robert Morrison</field >
<field name="gender">male</field >
<field name="city">Winterset</field >
<field name="balance">156.35</field >
</record>
<record>
<field name="stageName ">Spiderman </field >
<field name="realName">Peter Parker</field >
<field name="realName">male</field >
<field name="city">New York</field >
<field name="balance">-0.96</field >
</record>
</records>
Java Code listing
package com.northconcepts.datapipeline.examples.cookbook;
import java.io.File;
import com.northconcepts.datapipeline.core.DataReader;
import com.northconcepts.datapipeline.core.DataWriter;
import com.northconcepts.datapipeline.job.Job;
import com.northconcepts.datapipeline.pdf.PdfWriter;
import com.northconcepts.datapipeline.xml.SimpleXmlReader;
public class GenerateAPdf {
public static void main(String[] args) {
DataReader reader = new SimpleXmlReader(new File("example/data/input/simple-xml-input.xml"));
DataWriter writer = new PdfWriter(new File("example/data/output/simple-xml-output.pdf"));
Job.run(reader, writer);
}
}
Code walkthrough
- First a SimpleXmlReader is created corresponding to the input file i.e.
simple-xml-input.xml. - A PdfWriter is also created corresponding to the output file i.e.
simple-xml-output.pdf. - Data is transferred from the SimpleXmlReader to PdfWriter via Job.run() method.
Output
The output will be written to the PDF file and stored in the specified location i.e example/data/output.
