Get a Free Trial

Convert webpages and HTML to PDF

DownloadTechnical Information
ASP.NET API

When converting webpages and HTML to PDF GrabzIt's ASP.NET API provides the following features that help integrate GrabzIt into your system as easily as possible. However before you start remember that after calling the URLToPDF, HTMLToPDF or FileToPDF methods the SaveAsync or SaveToAsync method must be called to take the PDF screenshot.

Basic Options

A PDF screenshot will capture the entire webpage and will create a PDF document that is the same length as the original webpage. Only one parameter is required in order to convert a web page into a PDF document or to convert HTML to PDF as shown below.

grabzIt.URLToPDF("https://www.tesla.com");
//Then call the SaveAsync or SaveToAsync method
grabzIt.HTMLToPDF("<html><body><h1>Hello World!</h1></body></html>");
//Then call the SaveAsync or SaveToAsync method
grabzIt.FileToPDF("example.html");
//Then call the SaveAsync or SaveToAsync method

Custom Identifier

You can pass a custom identifier to the PDF methods as shown below, this value is then returned to your GrabzIt ASP.NET handler. For instance this custom identifier could be a database identifier, allowing a screenshot to be associated with a particular database record.

//The client should be stored somewhere and reused!
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret", client);

PDFOptions options = new PDFOptions();
options.CustomId = "123456";

grabzIt.URLToPDF("https://www.tesla.com", options);
//Then call the SaveAsync method
await grabzIt.SaveAsync("http://www.example.com/Home/Handler");
//The client should be stored somewhere and reused!
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret", client);

PDFOptions options = new PDFOptions();
options.CustomId = "123456";

grabzIt.HTMLToPDF("<html><body><h1>Hello World!</h1></body></html>", options);
//Then call the SaveAsync method
await grabzIt.SaveAsync("http://www.example.com/Home/Handler");
//The client should be stored somewhere and reused!
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret", client);

PDFOptions options = new PDFOptions();
options.CustomId = "123456";

grabzIt.FileToPDF("example.html", options);
//Then call the SaveAsync method
await grabzIt.SaveAsync("http://www.example.com/Home/Handler");

Headers and Footers

When you create a PDF screenshot you can state that you want to apply a particular template to the generated PDF. This template must be created in advance and will define the contents of the header and footer along with any special variables. In the example code below the user is applying the PDF template "my template".

If there is not a large enough top or bottom margin for the header or footer respectively, it will not appear in the PDF. In the below example we have set the top and bottom margins to 20 to provide plenty of space.

//The client should be stored somewhere and reused!
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret", client);

PDFOptions options = new PDFOptions();
options.MarginTop = 20;
options.MarginBottom = 20;
options.TemplateId = "my template";

grabzIt.URLToPDF("https://www.tesla.com", options);
//Then call the SaveAsync or SaveToAsync method
await grabzIt.SaveToAsync("result.pdf");
//The client should be stored somewhere and reused!
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret", client);

PDFOptions options = new PDFOptions();
options.MarginTop = 20;
options.MarginBottom = 20;
options.TemplateId = "my template";

grabzIt.HTMLToPDF("<html><body><h1>Hello World!</h1></body></html>", options);
//Then call the SaveAsync or SaveToAsync method
await grabzIt.SaveToAsync("result.pdf");
//The client should be stored somewhere and reused!
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret", client);

PDFOptions options = new PDFOptions();
options.MarginTop = 20;
options.MarginBottom = 20;
options.TemplateId = "my template";

grabzIt.FileToPDF("example.html", options);
//Then call the SaveAsync or SaveToAsync method
await grabzIt.SaveToAsync("result.pdf");

Convert HTML element to PDF

If you want to just convert a HTML element such as a div or span directly into a PDF document you can with GrabzIt's ASP.NET library. You must pass the CSS selector of the HTML element you wish to convert to the TargetElement parameter.

...
<span id="Article">
<p>This is the content I am interested in.</p>
<img src="myimage.jpg">
</span>
...

In this example, we wish to capture all the content in the span which has the id of Article, therefore we pass this to GrabzIt as shown below.

//The client should be stored somewhere and reused!
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret", client);

PDFOptions options = new PDFOptions();
options.TargetElement = "#Article";

grabzIt.URLToPDF("http://www.bbc.co.uk/news", options);
//Then call the SaveAsync or SaveToAsync method
await grabzIt.SaveToAsync("result.pdf");

How a PDF is cropped when targeting a HTML element can be controlled using these techniques.