Rank: Member Groups: Member
Joined: 8/26/2014 Posts: 20
|
When I use ConvertUrl to convert a local HTML file, I get an error if the file's extension is not htm. Is there a work around for this?
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,196
|
Hi, You can use something like this:
Code: C#
//Get the HTML text
string html = File.ReadAllText(filePath);
//This line is important if your HTML file uses other resources
//such as images, JavaScript, etc. Replace "c:/somepath" with
//the real folder of the HTML file
HtmlToPdf.Options.BaseUrl = "file:///c:/somepath/";
//Use ConvertHtml instead of ConvertUrl
HtmlToPdf.ConvertHtml(html, result_pdf_file);
Thanks!
|
Rank: Member Groups: Member
Joined: 8/26/2014 Posts: 20
|
So, that function requires the URL to have a htm extension? What other extensions are allowed? Thanks.
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,196
|
Hi,
It supports common known file types such as txt, htm, xml, jpg, etc. The reason that file extension is important is it determines the "mime" type of the page. For example, for the same HTML content if it has a "htm" extension then the browser will display it as HTML, but if it has a "txt" extension then the browser will display it as plain text without interpreting any HTML tags. When a page is loaded through HTTP, the web server sends the mime type in the HTTP header, so the extension does not matter. When a page is loaded from a file, the file extension is the only hint that can be used to derive the mime type. In this case, when your file has an unrecognized extension or no extension, the browser engine can not derive the mime type of the contents thus refuses to load it.
Thanks!
|