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 7650743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:19:53+00:00 2026-05-31T11:19:53+00:00

A code snippet is at the bottom of the question I am successfully able

  • 0

A code snippet is at the bottom of the question

I am successfully able to render a PDF in the client’s browser using a servlet and flying-saucer/iText. However, I can’t figure out how to get the style sheet included in the PDF rendering.

I’ve tried the following and none have worked thus far:

  • getServletContext().getRealPath("/PDFservlet.css") and putting "PDFservlet.css" at the web root directory
  • buf.append("<head><link rel='stylesheet' type='text/css' href='PDFservlet.css' /></head>") and putting "PDFservlet.css" at the web root directory, the directory where the servlet class file is and right under the "classes" directory
  • Same as above except for href='\PDFservlet.css' and putting "PDFservlet.css" in various places under my web root directory

Not sure what else to try here and how to get this CSS sheet recognized when the PDF is rendered in the client’s browser.

Can any of you tell me what I’m doing wrong here?

Also, I’m getting a java.io.IOException: Stream closed after the PDF is rendered and not sure where that’s coming from.

I’m testing this locally and running WebLogic Server 10.3.3.

public void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws javax.servlet.ServletException, java.io.IOException
{
    resp.setContentType("application/pdf");
    
    StringBuffer buf = new StringBuffer();
    buf.append("<html>");
    
    // put in some style
    buf.append("<head><link rel='stylesheet' type='text/css' href='PDFservlet.css' /></head>");
    
    // generate the rest of the HTML...
    buf.append("<body>");
    buf.append("    <div id='container'>");
    buf.append("        <div id='check_num'>1000</div>");
    buf.append("        <div id='address'><b>Estate Of JAMES SMITH</b><br />35 Addison Avenue<br />New York, NY 00000<br />(123)456-7890</div>");
    buf.append("        <div id='date'><i>Date</i>&#160;<u>02/08/2012</u></div>");
    buf.append("        <div id='void_message'><b>VOID 180 DAYS FROM CHECK DATE</b></div>");
    buf.append("        <div id='pay_line_container'>");
    buf.append("            <div id='pay_line_message'><i>Pay To The Order Of:</i></div>");
    buf.append("            <div id='pay_line'></div>");
    buf.append("            <div id='pay_line_pay_to'>Richard Richards XXIII</div>");
    buf.append("            <div id='pay_line_amount'>$&#160;5.00</div>");
    buf.append("        </div>");
    buf.append("        <div id='pay_line2_container'>");
    buf.append("            <div id='pay_line2'></div>");
    buf.append("            <div id='pay_line2_amount_description'>Five and 00/100</div>");
    buf.append("            <div id='pay_line2_dollars'>DOLLARS</div>");
    buf.append("        </div>");
    buf.append("        <div id='void_stamp'><b>VOID</b></div>");
    buf.append("        <div id='for_line'><i>For:</i>&#160;<u>test</u></div>");
    buf.append("        <div id='bank_info'><b>TD BANKNORTH</b><br />MAINE</div>");
    buf.append("        <div id='signature_line'></div>");
    buf.append("        <div id='bank_numbers'><b>c1000c a123456789a 987654321c</b></div>");
    buf.append("    </div>");
    buf.append("</body>");
    buf.append("</html>");
    
    System.out.println(buf.toString());
    
    // parse our markup into an xml Document
    try {
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(new StringBufferInputStream(buf.toString()));
        ITextRenderer renderer = new ITextRenderer();
        renderer.setDocument(doc, null);
        renderer.layout();
        OutputStream os = resp.getOutputStream();
        renderer.createPDF(os);
        os.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

Edit I

Reading the contents of the CSS file on the server side code into a String is working for me as an alternative (the readFile method is based off of Jon Skeet’s post at How do I create a Java string from the contents of a file?):

buf.append("<head><style>");
buf.append(readFile(getServletContext().getRealPath("/PDFservlet.css"), "UTF-8"));
buf.append("</style></head>");

Does this seem like an OK approach as an alternative?

Edit II

I’ve created a chat room for iText that I’m hoping some of you may take a look in every once in awhile. I’ve had several issues with iText/flying-saucer that I think may be easy to resolve with help from some of you experts. Please take a look if you get a chance and post whatever helpful material you might be able to for iText issues: https://chat.stackoverflow.com/rooms/8945/itext

  • 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-05-31T11:19:54+00:00Added an answer on May 31, 2026 at 11:19 am

    I decided with just reading my CSS file on the server side into a String.

    The readFile method is based off of Jon Skeet’s post at How do I create a Java string from the contents of a file?):

    buf.append("<head><style>");
    buf.append(readFile(getServletContext().getRealPath("/PDFservlet.css"), "UTF-8"));
    buf.append("</style></head>");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that I need to add the tracking code snippet at the bottom
(resolved: see bottom) I have the following code snippet: Protected Sub SqlDataSource1_Inserted(ByVal sender As
Code Snippet: ShippingPeriod[] arrShippingPeriods; . . . List<ShippingPeriod> shippingPeriods = ShippingPeriodList.ToList<ShippingPeriod>(); The Last line
Code snippet (normal pointer) int *pi = new int; int i = 90; pi
Code snippet.. if (regionalApprover == null) { throw new Exception(string.Format(The regional approver for {0}
This code snippet is from C# in Depth static bool AreReferencesEqual<T>(T first, T second)
Which code snippet will give better performance? The below code segments were written in
Relevant Code Snippet: public class MyPreference extends Preference { public MyPreference(Context context, AttributeSet attrs)
I have the following code snippet where some strings are initialized in the if
I have this JSP code snippet: <%@ taglib uri=http://java.sun.com/jsp/jstl/core prefix=c%> <c:choose> <c:when test=${var1.properties[\Item Type\]

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.