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();
}
Bellow is the code I finally found that works, there were a number of issues with the code above, but this seems to work: