We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Html to PDF CSS Example

Hey,

I seem to be missing something. I have attempted several tests and I cannot seem to get the external css to link up with my html file using aspose pdf.

Pdf pdf = new Pdf();
//Set Page Size
pdf.PageSetup.PageHeight = PageSize.LetterHeight;
pdf.PageSetup.PageWidth = PageSize.LetterWidth;
var margins = new MarginInfo();
margins.Top = 36;
margins.Left = 36;
margins.Right = 36;
margins.Bottom = 36;
pdf.PageSetup.Margin = margins;
pdf.HtmlInfo.ExternalResourcesBasePath = “C:/Users/Administrator/Documents/Visual Studio 2012/PDFTest/Content/html/”;
// load the HTML file to Stream object
using (Stream htmlAsStream = File.OpenRead(“C:/Users/Administrator/Documents/Visual Studio 2012/Projects/PDFTest/Content/html/index.html”))
{
// bind the source HTML
pdf.BindHTML(htmlAsStream, “”);
}
// save the PDF file
pdf.Save(“C:/Users/Administrator/Desktop/Aspose.pdf”);

my html file contains the following link ref =

I have also referenced several different examples on the Aspose website and they don’t help.

Thanks,
Grant

Hi Grant,


Thanks for your inquiry. Please check DOM approach for HTML to PDF conversion. You can pass a path argument as a string to HtmlLoadOption object for css/images database. Please check following code snippet for the purpose. Hopefully it will help you to accomplish the task.


// Specify the The base path/url for
the html file which serves as css/images database
<o:p></o:p>

String basePath = "E:/temp/";

HtmlLoadOptions htmloptions = new HtmlLoadOptions(basePath);

// load HTML file

Document doc22 = new Document("E:/temp/Input.html", htmloptions);

// Save HTML file

doc22.Save("E:/temp/output.pdf");


Please feel free to contact us for any further assistance.


Best Regards,