GrabzIt's Java API simplifies the process of converting HTML files, web pages, or HTML to PDF. It also allows you to easily integrate GrabzIt into your app.
However before you start remember that after calling the URLToPDF, HTMLToPDF or FileToPDF methods. You must call either the Save or SaveTo method to take the PDF screenshot or convert HTML straight to PDF.
A PDF screenshot captures the entire web page and converts it into a PDF file that can contain many pages. Depending on the length of the target web page. Converting a web page into a PDF document requires only one parameter. Or instead convert HTML to PDF as shown in the example below.
grabzIt.URLToPDF("https://www.tesla.com"); //Then call the Save or SaveTo method
grabzIt.HTMLToPDF("<html><body><h1>Hello World!</h1></body></html>"); //Then call the Save or SaveTo method
grabzIt.FileToPDF("example.html"); //Then call the Save or SaveTo method
You can pass a custom identifier to the PDF methods as shown below. Your GrabzIt Java handler then returns this value. For example, this special name could be a database ID, connecting a screenshot to a specific database entry.
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); PDFOptions options = new PDFOptions(); options.setCustomId("123456"); grabzIt.URLToPDF("https://www.tesla.com", options); //Then call the Save method grabzIt.Save("http://www.example.com/handler");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); PDFOptions options = new PDFOptions(); options.setCustomId("123456"); grabzIt.HTMLToPDF("<html><body><h1>Hello World!</h1></body></html>", options); //Then call the Save method grabzIt.Save("http://www.example.com/handler");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); PDFOptions options = new PDFOptions(); options.setCustomId("123456"); grabzIt.FileToPDF("example.html", options); //Then call the Save method grabzIt.Save("http://www.example.com/handler");
When you create a PDF screenshot you can request that you want to apply a particular template to the generated PDF. You must save this template in advance. This specifies the contents of the header and footer along with any special variables. In the example code below the user is using a template they created called "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.
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); PDFOptions options = new PDFOptions(); options.setMarginTop(20); options.setMarginBottom(20); options.setTemplateId("my template"); grabzIt.URLToPDF("https://www.tesla.com", options); //Then call the Save or SaveTo method grabzIt.SaveTo("result.pdf");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); PDFOptions options = new PDFOptions(); options.setMarginTop(20); options.setMarginBottom(20); options.setTemplateId("my template"); grabzIt.HTMLToPDF("<html><body><h1>Hello World!</h1></body></html>", options); //Then call the Save or SaveTo method grabzIt.SaveTo("result.pdf");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); PDFOptions options = new PDFOptions(); options.setMarginTop(20); options.setMarginBottom(20); options.setTemplateId("my template"); grabzIt.FileToPDF("example.html", options); //Then call the Save or SaveTo method grabzIt.SaveTo("result.pdf");
To just convert a single HTML element from an HTML document. Such as converting a single div or span directly into a PDF document you can with GrabzIt's Java library. You must pass the CSS selector of the HTML element you wish to convert to the setTargetElement
method.
... <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
. Set the target element by passing the id to GrabzIt.
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); PDFOptions options = new PDFOptions(); options.setTargetElement("#Article"); grabzIt.URLToPDF("http://www.bbc.co.uk/news", options); //Then call the Save or SaveTo method grabzIt.SaveTo("result.pdf");
Use these techniques to control the cropping of a PDF when targeting an HTML element.