Web page to PDF

Hi Team,


I tried to export web page into PDF exactly using the code as mentioned here Convert PDF documents using C# API|Aspose.PDF for .NET

But i am getting empty PDF with size 0bytes. Please find the attached file.

LEt me know how to export properly.

Thanks

Please let me know in case there is any update on my post.

Hi Pranav,


Thanks for contacting support.

Can you please share the input HTML file which you are using, so that we can test the scenario in our environment. We are sorry for this inconvenience.

Further exploring i am able to export HTML to PDF but I am not able to reference css file -

HTML file source

Test for Inline style and CSS support h1 {color:blue; display:inline;font-family: ‘Arial’} h2{text-decoration:underline;font-family: ‘Courier new’}a {color:pink; display:inline; text-decoration:italic; font-family: ‘Arial Black’}
Aspose.Pdf for .NET
This is sample Heading at level 2
some sample text with CSS support
Company overview

C# code

// Instantiate an object PDF class
Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf();
// add the section to PDF document sections collection
Aspose.Pdf.Generator.Section section = pdf.Sections.Add();
// Read the contents of HTML file into StreamReader object
StreamReader r = File.OpenText(@“C:\t1.htm”);
//Create text paragraphs containing HTML text
Aspose.Pdf.Generator.Text text2 = new Aspose.Pdf.Generator.Text(section, r.ReadToEnd());
// enable the property to display HTML contents within their own formatting
text2.IsHtmlTagSupported = true;
//Add the text paragraphs containing HTML text to the section
section.Paragraphs.Add(text2);
// Specify the URL which serves as images database
//pdf.HtmlInfo.ImgUrl = “D:/pdftest/MemoryStream/”;

//Save the pdf document
pdf.Save(“C:\HTML2pdf.pdf”);

Hi Pranav,

Thanks for sharing the details.

As per my observations, you are using legacy Aspose.Pdf.Generator approach for HTML to PDF conversion and there might be some issues when using older approach. However during my testing with new Document Object Model (DOM) approach of Aspose.Pdf namespace, I did not notice any problem and HTML file is properly being converted to PDF format. For your reference, I have attached the output generated over my end.

For further details, you may consider visiting Convert HTML to PDF Format

[C#]

// specify the path where referenced resources (CSS, Image) of HTML are present
HtmlLoadOptions load = new HtmlLoadOptions("c:/pdftest/");

// load source HTML file
Document pdfDoc = new Document("c:/pdftest/input.html", load);

// save PDF document
pdfDoc.Save("c:/pdftest/ResultantHTML.pdf");
I have used the same code as suggested by you.
Please find attached the PDF exported and the HTML I am using :

Test for Inline style and CSS support
h1 {color:blue; display:inline;font-family: 'Arial'}
h2{text-decoration:underline;font-family: 'Courier new'}
a {color:pink; display:inline; text-decoration:italic; font-family: 'Arial Black'}

Aspose.Pdf for .NET

This is sample Heading at level 2

Company overview

I have resolved it now. actually I was using the css file inside a folder.

Hi Pranav,


Thanks for the acknowledgement.

We are glad to hear that your problem is resolved. Please continue using our API’s and in the event of any further query, please feel free to contact.

At present I am able to export image as shown below:


. But in case image path is used the code is not able to export image. every time i have place the image where HTML is placed.

this does not work

Hi Pranav,

Thanks for sharing the details.

When referencing images in HTML file, we provide relative path instead of obsolete path and the path information of folder containing resources is passed as an argument to HtmlLoadOptions(…) object. Please take a look over following sample code and sample HTML file.

[HTML]

<html>
<head>

<title>Test for Inline style and CSS support </title>

<link href='C:\HTMLPDFTest\Styles\style.css' rel='stylesheet' type='text/css'/>

<style type='text/css'>

h1 {color:blue; display:inline;font-family: 'Arial'}

h2{text-decoration:underline;font-family: 'Courier new'}

a {color:pink; display:inline; text-decoration:italic; font-family: 'Arial Black'}

</style>

</head>

<body>

<h1>Aspose.Pdf for .NET</h1>

<h2>This is sample Heading at level 2</h2>

<a>some sample text with CSS support </a>

<div class='formHeading'>Company overview</div>

<img src="Header_Fine_Lines.PNG">

</body>

</html>

[C#]

// specify the path where referenced resources (CSS, Image) of HTML are present

HtmlLoadOptions load = new HtmlLoadOptions("c:/pdftest/");

// load source HTML file

Document pdfDoc = new Document("c:/pdftest/CV.html", load);

// save PDF document

pdfDoc.Save("c:/pdftest/ResultantHTML.pdf");

Please let me aslo know how to add a page break in the above code. I want few section of HTML to be exported in the new page of PDF

Hi Pranav,


Thanks for your patience.

In order to add page break, you need to define it in source HTML (using CSS) or you can split the HTML into two files and use our API to render HTML files in PDF format. Once the output is generated, you may consider concatenating PDF files. For more information, please visit Concatenate PDF Files