Free Support Forum - aspose.com

HTML to PDF

Hello,


I am trying to convert an existing html to PDF, the html has css files and images referenced in it.

Now, when I try to run my code as shown below, the generated PDF doesn’t show the images and css effects in it. The code is as shown below:

var dataDir = HttpContext.Current.Server.MapPath("/Template");

string basePath = dataDir + “\Output\”;

HtmlLoadOptions htmloptions = new HtmlLoadOptions(dataDir);

Document doc = new Document(dataDir + “\index.html”, htmloptions);

doc.Save(basePath + “output1.pdf”, Aspose.Pdf.SaveFormat.Pdf);


Please write back ASAP.

Regards,

Hi Ravindra,


Thanks for your inquiry.

Would you please share the input HTML file which you are using to generate PDF, so that we can test the scenario in our environment and address it accordingly. You can add your file to a zip archive and then share it by attaching with your post.


Best Regards,

Hi,


I have attached the html file for your reference. please change the extension to “.html”

Hi Ravindra,


Thanks for sharing file. I have checked the file but it seems that you have not shared CSS which has been referenced in the HTML, which is why it was not rendered correctly (Please check screenshot). Please share complete HTML with CSS file so that it can be rendered correctly and we can test scenario accordingly.

Best Regards,

Hi,


Is this a pdf file, if yes, then how did you include the folder path inside the index.html file ?

Regards,

Hi Ravindra,


Thanks for sharing more file. I have tried to convert your shared HTML into PDF by following code snippet, using Aspose.Pdf for .NET 17.5 and observed that in the output file, formatting of content was incorrect. For your reference, I have attached an output generated by below code.

FileStream htmlContentStream = new FileStream(dataDir + “index.html”, FileMode.Open);<o:p></o:p>

HtmlLoadOptions htmlLoadOptions = new HtmlLoadOptions(dataDir);

htmlLoadOptions.PageInfo.IsLandscape = true;

Document pdfDoc = new Document(htmlContentStream, htmlLoadOptions);

pdfDoc.Save(dataDir + "HTML2PDF_out.pdf");



I have logged an issue as PDFNET-42823 in our issue tracking system, for the sake of correction. We will further look into this and keep you updated with the status of its resolution. Please be patient and spare us little time.

ravindra.pathak:
Is this a pdf file, if yes, then how did you include the folder path inside the index.html file ?

It was not PDF file but a screenshot of how content was displayed when I opened your HTML into web browser.

Furthermore, in a case if your HTML contains images, you can use CustomLoaderOfExternalResources property to load/render those images inside PDF during conversion process. For more information you may visit "HTML to PDF - Resource Loading Callback" article in API documentation.

We are sorry for the inconvenience.


Best Regards,

Hi,


I have tried with the following code, but doesn’t seem to apply the css to the generated pdf, following is the code:

var dataDir = HttpContext.Current.Server.MapPath("/Template");

FileStream htmlContentStream = new FileStream(dataDir + “\indexsimple.html”, FileMode.Open);
HtmlLoadOptions htmlLoadOptions = new HtmlLoadOptions(dataDir);
htmlLoadOptions.PageInfo.IsLandscape = true;
Document pdfDoc = new Document(htmlContentStream, htmlLoadOptions);
pdfDoc.Save(dataDir + “\HTML2PDF_out.pdf”);


Have you done any changes to the index.html file, like changing of the path of css etc. ?

I have attached the generated pdf for your reference.

Also, thank you for the advice on using <span class=“pl-en” style=“color: rgb(121, 93, 163); font-family: SFMono-Regular, Consolas, “Liberation Mono”, Menlo, Courier, monospace; font-size: 12px; white-space: pre; background-color: rgb(255, 255, 255);”>CustomLoaderOfExternalResources<span style=“color: rgb(36, 41, 46); font-family: SFMono-Regular, Consolas, “Liberation Mono”, Menlo, Courier, monospace; font-size: 12px; white-space: pre; background-color: rgb(255, 255, 255);”> property, but the sample link provided uses only one image,
<span style=“color: rgb(36, 41, 46); font-family: SFMono-Regular, Consolas, “Liberation Mono”, Menlo, Courier, monospace; font-size: 12px; white-space: pre; background-color: rgb(255, 255, 255);”>if I have multiple images included in the html, how can I load them all while the conversion is in process ?
<span style=“color: rgb(36, 41, 46); font-family: SFMono-Regular, Consolas, “Liberation Mono”, Menlo, Courier, monospace; font-size: 12px; white-space: pre; background-color: rgb(255, 255, 255);”>
Regards,

Hello,


I have generated the pdf using some other logic, but the images and alignment is not properly aligned, could you please suggest on this ASAP.

I have attached the ActualLetter.png and the pdf generated for your reference.

The “Total” part goes in the next page and the table is truncated and aligned to the left.

Regards,

Hi,


could you please reply on this ASAP as I have deliverable waiting because of this issue.

Regards,
Ravindra R Pathak

Hi Ravindra,


Thanks for the feedback.

Can you please share a sample project which can help us to replicate the above stated issue. We will test the scenario and will reply accordingly. We are sorry for this inconvenience.

Hello,


PFA the sample project, it is a Webapi project, please use post man to call the url for e.g. “http://localhost:60877/api/pdfgenerator” and use the POST method in postman, the pdf file is generated in the “Template” folder.

Please write back in case of any queries.

I have removed the package folder due to large size, the packages.config file is included for you to download from nuget.

Thanks.

Regards,
Ravindra R Pathak

Hi Ravindra,


We are working on testing the scenario using shared project and will keep you updated with our findings.

Hi Ravindra,


Thanks for sharing sample project.

I have tested the scenario again by running your project and observed the issue. Please note that we have already noticed the irregularities and misalignment of content in the generated output, hence logged an issue with ID PDFNET-42823 in our issue tracking system, which was shared above as well.

We will definitely look into the details of the issue and keep you posted with the status of its rectification. Please be patient and spare us little time. We are sorry for the inconvenience.


Best Regards,

How can I generate a pdf format file from the HTML file of the coding of the website hp customer service ??? I want to have the script in pdf format.

@kevingomes

Thanks for your inquiry.

You can please download source of a web page using WebRequest Class and add source text inside PDF using TextFragment. Please check following code snippet and attached PDF document for your reference:

var url = "https://hpsupports.co/hp-customer-support/";
System.Net.WebRequest request = System.Net.WebRequest.Create(url);
// If required by the server, set the credentials.
request.Credentials = System.Net.CredentialCache.DefaultCredentials;
// Time out in miliseconds before the request times out
// Request.Timeout = 100;
// Get the response.
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();

Document pdfDocument = new Document();
TextFragment textFragment = new TextFragment(responseFromServer);
pdfDocument.Pages.Add();
pdfDocument.Pages[1].Paragraphs.Add(textFragment);
// Save output as PDF format
pdfDocument.Save(dataDir + "WebpageToPdf.pdf");

WebpageToPdf.pdf (83.2 KB)

In case of any further assistance, please feel free to let us know.