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"?>
    <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 >

    <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 >

Java Code listing

package com.northconcepts.datapipeline.examples.cookbook;


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"));, writer);


Code walkthrough

  1. First a SimpleXmlReader is created corresponding to the input file i.e. simple-xml-input.xml.
  2. A PdfWriter is also created corresponding to the output file i.e. simple-xml-output.pdf.
  3. Data is transferred from the SimpleXmlReader to PdfWriter via method.


The output will be written to the PDF file and stored in the specified location i.e example/data/output.

Mobile Analytics