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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:16:51+00:00 2026-06-13T22:16:51+00:00

I am stuck trying to debug some code that is designed to convert an

  • 0

I am stuck trying to debug some code that is designed to convert an web page into a PDF document via a string variable. It uses the iTextSharp c# tool (xmlworker) and is a modification of the example code that comes with the sourceforge source code called html2pdf.csproj. This example code converts an existing html file into a PDF file and saves it in the same directory as the file that was converted. I have a string variable containing html formatted text and I am trying to make it able to be returned as a byte array that will be passed to the client side in a web environment for printing purposes. The problem is that I get an “IOException was unhandled by user code” message that states “The document has no pages.” I’m a little unsure what this is suppose to mean, nor how to go about diagnosing the problem. The example code using the file based system works and I have successfully converted a static version of the html string to PDF. Below is the modified code:

       private byte[] createPDF(string html, string filename) {
            MemoryStream msOutput = new MemoryStream();
            string printPDFCSS = Server.MapPath("/content/printPDF.css");
            Document doc = new Document(PageSize.LETTER);
            doc.SetMargins(doc.LeftMargin, doc.RightMargin, 35, 0);
            PdfWriter pdfWriter = PdfWriter.GetInstance(doc, msOutput);

            doc.Open();
            Dictionary<String, String> substFonts = new Dictionary<String, String>();
            substFonts["Arial Unicode MS"] = "Helvetica";
            CssFilesImpl cssFiles = new CssFilesImpl();
            cssFiles.Add(XMLWorkerHelper.GetCSS(new FileStream(printPDFCSS, FileMode.Open)));
            StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles);
            HtmlPipelineContext hpc = new HtmlPipelineContext(new CssAppliersImpl(new UnembedFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS, substFonts)));
            hpc.SetImageProvider(new ImageProvider(filename));
            hpc.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(Tags.GetHtmlTagProcessorFactory());
            HtmlPipeline htmlPipeline = new HtmlPipeline(hpc, new PdfWriterPipeline(doc, pdfWriter));
            IPipeline pipeline = new CssResolverPipeline(cssResolver, htmlPipeline);
            XMLWorker worker = new XMLWorker(pipeline, true);
            XMLParser xmlParse = new XMLParser(true, worker);
            xmlParse.Parse(msOutput);
            doc.Close();
            return msOutput.ToArray();
        }
  • 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-13T22:16:52+00:00Added an answer on June 13, 2026 at 10:16 pm

    Bellow is the code I finally found that works, there were a number of issues with the code above, but this seems to work:

    private byte[] createPDF(string html, string filename){
            MemoryStream msInput = new MemoryStream(ASCIIEncoding.Default.GetBytes(html));
            MemoryStream msOutput = new MemoryStream();
            string printPDFCSS = Server.MapPath("/content/printPDF.css");
            Document doc = new Document(PageSize.LETTER);
            doc.SetMargins(doc.LeftMargin, doc.RightMargin, 35, 0);
            PdfWriter pdfWriter = PdfWriter.GetInstance(doc, msOutput);
            doc.Open();
            Dictionary<String, String> substFonts = new Dictionary<String, String>();
            substFonts["Arial Unicode MS"] = "Helvetica";
            CssFilesImpl cssFiles = new CssFilesImpl();
            cssFiles.Add(XMLWorkerHelper.GetCSS(new FileStream(printPDFCSS, FileMode.Open)));
            StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles);
            HtmlPipelineContext hpc = new HtmlPipelineContext(new CssAppliersImpl(new UnembedFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS, substFonts)));
            hpc.SetImageProvider(new ImageProvider(filename));
            hpc.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(Tags.GetHtmlTagProcessorFactory());
            HtmlPipeline htmlPipeline = new HtmlPipeline(hpc, new PdfWriterPipeline(doc, pdfWriter));
            IPipeline pipeline = new CssResolverPipeline(cssResolver, htmlPipeline);
            XMLWorker worker = new XMLWorker(pipeline, true);
            XMLParser xmlParse = new XMLParser(true, worker);
            xmlParse.Parse(msInput);
            doc.Close();
            return msOutput.ToArray();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sometimes I still get stuck trying to translate procedural code into functional code. Is
I'm trying to debug some touchesBegan/Moved/Ended related slowdown in my game; I think that
I am trying to debug some code related to ListView on Samsung Captivate (Galaxy
I have a tough scenario I'm trying to debug... On a web forms page,
After putting some code to work, I'm trying to optimize it. One thing that
I'm trying to debug an access violation error in some native code, and I
I'm trying to debug a problem with some legacy code. While trying to understand
I'm stuck trying to get a WinInet HTTP POST via SSL using ONLY C.
I'm trying to: ((Request.Params[crmid] != null)) in a web page. But it keeps throwing
Could use some help. I am utterly stuck, been trying to fix this one

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.