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

  • SEARCH
  • Home
  • 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 8878447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:42:49+00:00 2026-06-14T19:42:49+00:00

Here is a snippet of my Servlet code which generates a PDF and then

  • 0

Here is a snippet of my Servlet code which generates a PDF and then opens it. It can not open the “AvtoSolaZaposleniXSL.xsl” file. If I run the same procedure in a normal Java class everything runs smooth.

public class PDF extends HttpServlet {
private static final long serialVersionUID = 1L;

public PDF() { super(); }

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    //Generiraj PDF
    File xmlfile = new File(getServletContext().getRealPath("AvtoSolaZ.xml"));
    File xsltfile = new File(getServletContext().getRealPath("AvtoSolaZaposleniXSL.xsl"));      
    ExampleXML2PDF.generirajPDF(xmlfile, xsltfile);

    //Počakaj da se v miru zgenerira PDF
    try {
        Thread.sleep(5000L);
    } catch (InterruptedException e1) {
        e1.printStackTrace();
    }

    //Zaženi pdf
    File f1 = new File(getServletContext().getRealPath("Avtosola.pdf"));
    String pdfKoncni = f1.toString();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfReader reader = new PdfReader(pdfKoncni);
    PdfStamper stamper = null;
    try {
        stamper = new PdfStamper(reader, baos);
    } catch (DocumentException e) {
        e.printStackTrace();
    }
    try {
        stamper.close();
    } catch (DocumentException e) {
        e.printStackTrace();
    }

    // set some response headers
    response.setHeader("Expires", "0");
    response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
    response.setHeader("Pragma", "public");
    response.setContentType("application/pdf");
    response.setContentLength(baos.size());

    OutputStream os = response.getOutputStream();
    baos.writeTo(os);
    os.flush();
    os.close();
}

All my files are in the WebContent folder and my Servlet in a default package.

Error:

(Location of error unknown)java.io.FileNotFoundException: C:\Eclipse\eclipse\WebContent\AvtoSolaZaposleniXSL.xsl (The system cannot find the file specified)
java.lang.NullPointerException

Believe me that I have searched for an answer like this and haven’t found anything that could actually help me. Even if I put the whole path (Which is not C:\Eclipse\eclipse… and I don’t know why it states that way..) it still does not work.

Like I said. If I run it in a normal Java class it generates the PDF normally and works just fine…

import java.io.File;

public class Test {

public static void main(String[] args) {        

    File xmlfile = new File("WebContent/AvtoSolaZ.xml");
    File xsltfile = new File("WebContent/AvtoSolaZaposleniXSL.xsl");
    ExampleXML2PDF.generirajPDF(xmlfile, xsltfile);
}
}

Please help!

  • 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-14T19:42:50+00:00Added an answer on June 14, 2026 at 7:42 pm

    I re-thinked my problem and came up with an easier solution on how to display a pdf which was generated. I just used my class, which generates a pdf(XSL-FO transformation, for which an xml and xsl is needed..) and copied the code into the servlet. Adjusted the output stream and voula!

    Here is the code of the new servlet which works perfectly fine:

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        try {
    
            response.setContentType("application/pdf"); 
    
            // Setup directories
            File xmlfile = new File(getServletContext().getRealPath("/AvtoSolaZ.xml"));
            File xsltfile = new File(getServletContext().getRealPath("/AvtoSolaZaposleniXSL.xsl")); 
            File outDir = new File("WebContent");
            outDir.mkdirs();
    
            // configure fopFactory as desired
            FopFactory fopFactory = FopFactory.newInstance();
    
            FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
            // configure foUserAgent as desired
    
            // Setup output
            OutputStream out = response.getOutputStream();
    
            try {
                // Construct fop with desired output format
                Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
    
                // Setup XSLT
                TransformerFactory factory = TransformerFactory.newInstance();
                Transformer transformer = factory
                        .newTransformer(new StreamSource(xsltfile));
    
                // Set the value of a <param> in the stylesheet
                transformer.setParameter("versionParam", "2.0");
    
                // Setup input for XSLT transformation
                Source src = new StreamSource(xmlfile);
    
                // Resulting SAX events (the generated FO) must be piped through
                // to FOP
                Result res = new SAXResult(fop.getDefaultHandler());
    
                // Start XSLT transformation and FOP processing
                transformer.transform(src, res);
    
            } finally {
                out.close();
            }
    
        } catch (Exception e) {
            e.printStackTrace(System.err);
            System.exit(-1);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found this code snippet here , but I just can't figure out what
I found a code snippet here , which I wanted to use, but I
Here's a snippet of code I saw on the web and I'm wondering if
Here is a snippet of my code $fp = fsockopen($s['url'], 80, $errno, $errstr, 5);
So here's a snippet of my code. void RoutingProtocolImpl::removeAllInfinity() { dv.erase(std::remove_if(dv.begin(), dv.end(), hasInfCost), dv.end());
So here's a snippet of my code: struct dv_nexthop_cost_pair { unsigned short nexthop; unsigned
I found a nice snippet here on Stack which grabs the text from a
Here's a snippet from my application: class PortolioItem(models.Model): ... user = models.ForeignKey(User) contract =
Here's a snippet of a script of mine attempting to put extra Unicode awesomeness
Here is a snippet of the official Apple Documentation of AudioBufferList (Core Audio Data

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.