Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8860057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:11:36+00:00 2026-06-14T15:11:36+00:00

I’m trying to create a PDF document with Apache FOP from an xml file

  • 0

I’m trying to create a PDF document with Apache FOP from an xml file that is formatted with an xlst stylesheet to convert the original xml file to an xml-fo formatted xml file.
As I’m new to this, I tried to create a simple hello world example, without success.

The generation process seems to succeed (no exceptions) but the generated pdf file is invalid for some reason.
The size of the file is 4.8KB, and when opened with libreoffice writer, data has definitely been written to the file, but the file does not open in pdf readers.

The XML file is rather straightforward:

<rentalRequest>
  <firstName>foo</firstName>
  <lastName>bar</lastName>
  <email>foo@bar.com</email>
  <street>foo street</street>
  <houseNo>42</houseNo>
  <postalCode>4242</postalCode>
  <city>bar city</city>
</rentalRequest>

The XSL file, simply attempting to print Hello World!:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                xmlns:fo="http://www.w3.org/1999/XSL/Format">

    <xsl:output method="xml" indent="yes" />

    <xsl:template match="/">
        <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
            <fo:layout-master-set>          
                <fo:simple-page-master master-name="all">
                    <fo:region-body />
                </fo:simple-page-master>
            </fo:layout-master-set>

            <fo:page-sequence master-reference="all">
                <fo:flow flow-name="xsl-region-body">
                    <fo:block>
                        Hello World!
                    </fo:block>
                </fo:flow>
            </fo:page-sequence>
        </fo:root>
    </xsl:template>

</xsl:stylesheet>

The java code to generate a pdf file using JAXP + FOP:

public void buildWithXSL(String xml) throws Exception {
        OutputStream out = new BufferedOutputStream(new FileOutputStream(new File("rentalrequest.pdf")));

        // setup xml input source
        StreamSource xmlSource =
                new StreamSource(new ByteArrayInputStream(xml.getBytes()));

        File xslFile = new File("src/main/resources/xml/rentalrequest2fo.xsl");
        FileInputStream xslFileStream = new FileInputStream(xslFile);
        StreamSource xslSource = new StreamSource(xslFileStream);

        TransformerFactory tfactory = TransformerFactory.newInstance();
        Transformer transformer = tfactory.newTransformer(xslSource);

        FopFactory fopFactory = FopFactory.newInstance();
        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);

        // perform transformation 
        Result res = new SAXResult(fop.getDefaultHandler());
        transformer.transform(xmlSource, res);
    }

The generated pdf file can be found at http://www.filedropper.com/rentalrequest

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:11:37+00:00Added an answer on June 14, 2026 at 3:11 pm

    You got an incomplete PDF file because you forgot to properly close the OutputStream. Always use the following pattern when you open an OutputStream (or InputStream for that matter):

    OutputStream out = new [Something]OutputStream([something]);
    try {
        //write to the OutputStream
    } finally {
        out.close();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I am trying to render a haml file in a javascript response like so:
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm parsing an XML file, the creators of it stuck in a bunch social
I have a bunch of posts stored in text files formatted in yaml/textile (from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.