Hi Team,
Please let me know in case there is any update on my post.
Hi Pranav,
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");
Aspose.Pdf for .NET
This is sample Heading at level 2
I have resolved it now. actually I was using the css file inside a folder.
Hi Pranav,
At present I am able to export image as shown below:
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,