How can I convert an ASPX page- including external images/CSS- into PDF?

Within my .NET 4.0 ASP.NET web application I’m trying to convert an ASPX page into PDF using Aspose PDF, but it looks like no external resource references are being resolved and included into the generation by the component.

How can I have my ASPX pages be converted into PDF just like they would be displayed on screen?

Hi John,

Thanks for your inquiry. While rendering HTML to PDF you can use absolute and relative reference for CSS files. Images database can be specified using ImgUrl property of HtmlInfo class. Please check following documentation links for details. Hopefully it will serve the purpose. If issue persist then please share your sample code and pages here. So we will look into it and will provide you more information.

Please feel free to contact us for any further assistance.

Best regards,

Dear Tilal,

thank you for your response.

Unfortunately the steps suggested don’t seem to work. I’m currently only getting an empty page.

I’ve attached the code I’m using to create the PDF.

Although I’m outputting the PDF manually in the .aspx.cs file, I as well could have been using Pdf.Save(string fileName, SaveType saveType, HttpResponse response). That function yields an empty page, too.

Same for Pdf.BindHTML().

Just to make sure, I should add that in my code, htmlWriter.ToString() yields valid HTML. So the HTML text used as a source is not the reason for the problem here.

Can you please help me? What am I doing wrong?

Hi John,


Thanks for your feedback. Can you please share your sample source html and other resources (images/css) here? So we will test the scenario at our end and will provide you more information accordingly.

Best Regards,

Thanks for trying to help.

I already attached all the necessary files. No matter which input I provide, no output is created by Aspose Pdf.

Just for testing purposes I’ve once been using the following hard coded literal text (please refer to the attached files for reference):

Aspose.Pdf.Generator.Text text = new Aspose.Pdf.Generator.Text(section, @“Test”);

Even that didn’t yield any visible result.


We are using Aspose.Pdf file version 8.3.0.0, product version 2013.08.01 here.

Hi John,


Thanks for your feedback. I’m afraid your are initiating section object but not adding it to PDF document so getting empty document. Please amend your code as following. Hopefully your issue will be resolved.

Aspose.Pdf.Generator.Section section = new Aspose.Pdf.Generator.Section();
pdf.Sections.Add(section);
Aspose.Pdf.Generator.Text text = new Aspose.Pdf.Generator.Text(section, htmlWriter.ToString());

Please feel free to contact us for any further assistance.

Best Regards,

Hi Tilal,

d’oh… that sounds very reasonable to me. I’ll try to fix that as soon as I’m back in the office tomorrow.

Allow me one more question before closing this issue: Why isn’t Pdf.BindHTML() working? I’d been assuming that this function encapulated all the other code into one single vanilla function call.

Hi John,

Thanks for your feedback. BindHTML() needs a valid HTML string as parameter instead of file path. Please check following code snippet used to generate PDF document. Hopefully it will serve the purpose. If issue persist then please share your sample source HTML. So we will test the scenario at our end and will provide you more information.

Pdf pdf = new Pdf();
//string html = File.ReadAllText(myDir + “HTMLTEXT.html”);
//pdf.BindHTML(html);
pdf.BindHTML(@“Test”);
pdf.Save(myDir + “HtmltoPdf_out.pdf”);

Please feel free to contact us for any further assistance.

Best Regards,

Thanks for your excellent help, Tilal!

That had been the problem. I now can see HTML, CSS and images.

But now I’m stumbeling over a number of other issues:

  • The property IsLandscape doesn’t seem to work. Although I’m setting it to true, the resulting PDF document still is in portrait mode. What am I doing wrong here?

  • Which CSS media type is getting used for the PDF document?

  • The font/image sizes differ badly from the printed web page. What do I need to do in order to get fonts/images resemble the version printed by IE?


For you to be able to anser my third question, I have uploaded a sample HTML output file. I hope it can help you find out what’s missing so you may be able to help me with this issue?

I should add that I can’t edit the CSS files. Particularly default.css is getting used throughout the whole application so far.


I very much appreciate your help on this. I urgently need to deliver the pdf result.

SetTrend:

  • The property IsLandscape doesn’t seem to work. Although I’m setting it to true, the resulting PDF document still is in portrait mode. What am I doing wrong here?
Hi John,

I have tested the scenario using Aspose.Pdf for .NET 8.3.0 where I have used the following code snippet to generate the PDF file and as per my observations, resultant file is being generated in landscape mode.
[C#]

Pdf pdf = new Pdf();<o:p></o:p>

pdf.IsLandscape = true;

pdf.BindHTML(@"Test");

pdf.Save(“c:/pdftest/HtmltoPdf_out.pdf”);

SetTrend:

SetTrend:
Can you please share some further details regarding this requirement.
  • The font/image sizes differ badly from the printed web page. What do I need to do in order to get fonts/images resemble the version printed by IE?
  • I have tested the scenario and I am able to
    notice the same problem. For the sake of correction, I have logged this issue
    as PDFNEWNET-35771 in our issue tracking system. We will
    further look into the details of this problem and will keep you updated on the
    status of correction.

    SetTrend:
    I should add that I can’t edit the CSS files. Particularly default.css is getting used throughout the whole application so far.
    Please visit the following link for further details on CSS referencing during conversion. Please note that in order to apply the CSS, the path of CSS files should be provided in source HTML and relative path is specified using pdf.HtmlInfo.ExternalResourcesBasePath property.

    The issues you have found earlier (filed as PDFNET-35771) have been fixed in Aspose.PDF for .NET 22.11.