HTML Converter Local Resources
If you have not reviewed using base urls with HTML Converter, then you should first review that topic. This page presents an example of how you can use local resources with HTML converter.
HTML Converter has the same expectations of any HTML page, relative links are resolved in relation to the web page' address. However, you might need to work with webpages and resources that are on a local file directory rather than a page served using a web server.
If you are unfamiliar with using base urls, then refer to the BaseURLs documentation topic.
Example - Local File and Local CSS
Suppose you have an HTML document with a relative URL to a CSS file, both of which reside in a local directory. Here are two easy ways to render the HTML page as a PDF using HTML Converter.
Consider the following HTML page and CSS stylesheet.
<html>
<head>
<link rel="stylesheet" type="text/css" href="./example.css" />
</head>
<body>
<h1>Welcome to DynamicPDF HTML Converter.</h1>
<p>An example of embedded CSS stylesheet.</p>
</body>
<html>
body {
background-color: lightblue;
}
h1 {
color:red;
}
p {
font-family: verdana;
font-size: 20px;
color:green;
}
Load HTML Page using URI
In this first example you load the HTML page using a URI, only you specify the URI scheme as file which resolves to file:///<host>/<path>
.
public static void FromFile()
{
Uri uri = new Uri(new Uri("file://"), Util.GetPath("./Resources/example.html").ToString());
Converter.Convert(uri, outputPath);
}
Public Shared Sub FromFile()
Dim uri As New Uri(New Uri("file://"), Util.GetPath("./Resources/example.html").ToString())
Converter.Convert(uri, ouputPath)
End Sub
The relative path to the CSS document is resolved when converting the HTML to PDF.
Figure 1. Converting HTML with local CSS.
Load HTML Page as Text
Another way you can work with local HTML and resources is by specifying a base URI for the directory containing the CSS stylesheet. Then in the Convert
method use one of the overloads that take a path to the base URI.
Loading the HTML as a text string and supplying it to the Convert method results in the CSS style not being applied to the HTML. The reason for this is because HTML Converter has no context for the relative path to the CSS file.
public static void FromHtml()
{
Uri uri = new Uri(new Uri("file://"), Util.GetPath("./Resources/").ToString());
string html = "<html><head><link rel = \"stylesheet\" type = \"text/css\" href = \"./example.css\" />"
+ "</head><body><h1>Welcome to DynamicPDF Html Converter.</h1>"
+ "<p>An example of embedded CSS stylesheet.</p>"
+ "</body></html>";
Converter.Convert(html,outputPath, uri);
}
Public Shared Sub FromHtml()
Dim uri As New Uri(New Uri("file://"), Util.GetPath("./Resources/").ToString())
Dim html As String = "<html><head><link rel = 'stylesheet' type = 'text/css' href = './example.css' />" &
"</head><body><h1>Welcome to DynamicPDF Html Converter.</h1>" &
"<p>An example of embedded CSS stylesheet.</p>" &
"</body></html>"
Converter.Convert(html, outputPath, uri)
End Sub
Local Image
You are not limited to only CSS stylesheets, you can load any local resources using the technique in the example above. For example, you can load the HTML as a URI and then rely on the relative paths to render the image, or you can set the HTML document's base path.
public static void FromFile()
{
Uri uri = new Uri(new Uri("file://"), Util.GetPath("./Resources/products.html"));
Converter.Convert(uri, Util.GetPath("Output/ImageLocalExample-file-out.pdf"));
}
public static void FromHtml()
{
Uri uri = new Uri(new Uri("file://"), Util.GetPath("./Resources/"));
string html = File.ReadAllText(Util.GetPath("./Resources/products.html"));
Converter.Convert(html, Util.GetPath("Output/ImageLocalExample-raw-out.pdf"), uri);
}
public static void FromFile()
{
Uri uri = new Uri(new Uri("file://"), Util.GetPath("./Resources/example.html").ToString());
Converter.Convert(uri, Util.GetPath("Output/CssFileConversion-file-out.pdf"));
}
public static void FromHtml()
{
Uri uri = new Uri(new Uri("file://"), Util.GetPath("./Resources/").ToString());
string html = "<html><head><link rel = \"stylesheet\" type = \"text/css\" href = \"./example.css\" />"
+ "</head><body><h1>Welcome to DynamicPDF Html Converter.</h1>"
+ "<p>An example of embedded CSS stylesheet.</p>"
+ "</body></html>";
Console.WriteLine(html);
Converter.Convert(html, Util.GetPath("Output/CssFileConversion-raw-out.pdf"), uri);
}
Figure 2. Converting HTML with local image.