Rank: Newbie Groups: Member
Joined: 9/8/2014 Posts: 2
|
I am using HtmlToPdf.ConvertUrl to convert an aspx url into a pdf document. When this is first done the result is as expected with 2 pages returned in a Pdf Document. When I execute it again I get a Pdf Document with 40 pages. The document is broken up into 40 pages with one line of html per page. If I try and then covert a html string to pdf I also get many pages.
What is causing this?
Here is my method for doing this public static byte[] ApplicantPrint(Int32 poid, List<String> paids, String imageUrl, String documentDirectoryPath, Boolean bIncludeSummary = true) { String url; MemoryStream oStream;
SetEOPdfLicense(); HtmlToPdf.Options.PageSize = PdfPageSizes.A4;
var oMergeDoc = new PdfDocument();
if (bIncludeSummary) { //Create a summary page url = imageUrl + "ViewApplicationSummaryPdf.aspx?poId=" + EncryptionBase64.EncryptToBase64(poid.ToString()) + "&paIds=" + EncryptionBase64.EncryptToBase64(String.Join("-", paids.ToArray()));
try { HtmlToPdf.Options.NoCache = true; HtmlToPdf.Options.NoScript = true; HtmlToPdf.ConvertUrl(url, oMergeDoc); } catch (Exception ex) { HtmlToPdf.ConvertHtml("<b>PDF print failed for Applicant: </b> [" + ex.Message + "]<br>", oMergeDoc); } }
//Add the individual pages foreach (var paid in paids) { try { url = imageUrl + "ViewApplicationPdf.aspx?Id=" + EncryptionBase64.EncryptToBase64(paid); HtmlToPdf.ConvertUrl(url, oMergeDoc);
var sDocList = PositionApplicationSupportingDocument.GetDocumentByType(Utility.UiValueToIntNotNull(paid), SupportingMaterialTypeObject.SupportingMaterialTypes.Resume); if (sDocList.Count > 0) { var resumeDoc = sDocList[0]; var supportingMaterialObject = SupportingMaterial.GetById(resumeDoc.SupportingDocumentId); if (supportingMaterialObject != null) { var path = documentDirectoryPath + supportingMaterialObject.FileName; var resume = new FileInfo(path); if (resume.Exists) { var fileExt = Path.GetExtension(path);
switch (fileExt) { case ".docx": case ".doc": oStream = new MemoryStream(PrintFromDoc(ReadFile(path), supportingMaterialObject.FileName)); oMergeDoc = PdfDocument.Merge(oMergeDoc, new PdfDocument(oStream)); break; case ".pdf": var oDoc = new PdfDocument(new MemoryStream(ReadFile(path))); oMergeDoc = PdfDocument.Merge(oMergeDoc, oDoc); break; case ".gif": case ".png": case ".jpeg": case ".tiff": case ".jpg": var page = oMergeDoc.Pages.Add(); oStream = new MemoryStream(ReadFile(path)); var oImage = Image.FromStream(oStream); var pdfImage = new EO.Pdf.Drawing.PdfImage(oImage); var content = new EO.Pdf.Contents.PdfImageContent(pdfImage);
if (oImage.Size.Width > 612 || content.Image.Image.Size.Height > 792) // resize the image to fit an A4 page content.GfxMatrix.Scale(612, 792); else { content.GfxMatrix.Translate(0, 0); content.AutoScale(); }
page.Contents.Add(content); break; } } } } } catch (Exception ex) { HtmlToPdf.ConvertHtml("<b>Unable to convert file to PDF: " + ex.Message + "</b><br>", oMergeDoc); } }
//Add footer to each page oStream = new MemoryStream(); oMergeDoc.Save(oStream);
var oData = AddFooter(oStream.ToArray()); oStream.Dispose();
return oData; }
public static Byte[] AddFooter(Byte[] oData) { var oStream = new MemoryStream(oData); var oDoc = new PdfDocument(oStream);
var totalPages = oDoc.Pages.Count; for (var i = 0; i < totalPages; i++) { //Set the output area to the footer area HtmlToPdf.Options.OutputArea = new RectangleF(0, 11, 8.0f, 0.5f);
//Output whatever footer contents in the footer area //Note that the second argument is a PdfPage object HtmlToPdf.ConvertHtml(String.Format("<div style=\"text-align:right; padding-right:20px;\">Page {0} of {1}</div>", i + 1, totalPages), oDoc.Pages[i]); }
oStream = new MemoryStream(); oDoc.Save(oStream); var oDataBytes = oStream.ToArray(); oStream.Dispose();
return oDataBytes; }
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,196
|
Hi, Your code looks fine. You can try to save the result HTML into a file, then convert that file to see if you get the same result. Most likely you have forced page break somewhere in your HTML: http://www.essentialobjects.com/doc/4/htmltopdf/paging.aspxThanks!
|