Get a Free Trial

How to Control the Cropping of PDF Content?

When an HTML element is targeted in a PDF conversion the result is cropped to show only the HTML element. However, the question remains as to what to do with the rest of the page. Should the full page be shown, with the targeted HTML element at the top or just the HTML element?

By default the full page is shown with the targeted HTML element shown at the top of the page. As shown in the example to the right.

However sometimes a PDF is required to be generated that would only show the selected HTML element without the rest of the page. An example of what this would look like is shown to the left.

In order to do this you must pass a -1 to the page width and height parameters. Unfortunately however this approach is more complicated because before the PDF is cropped it must be rendered.

So if the default page size is not large enough to nicely render the HTML element you may need to set a larger page size and/or orientation to ensure it is properly rendered as required. For instance try a page size of A3 or B3 etc and perhaps an orientation of landscape.

Automatically Crop the Page Size to the HTML Target

Note that the auto-sizing page size feature will work only when a HTML element is targeted.

//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";
options.PageWidth = -1;
options.PageHeight = -1;
grabzIt.URLToPDF("https://www.bbc.com", options);
await grabzIt.SaveAsync("https://www.mywebsite.com/Home/Handler");
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");
options.setPageWidth(-1);
options.setPageHeight(-1);
grabzIt.URLToPDF("https://www.bbc.com", options);
grabzIt.Save("https://www.mywebsite.com/handler");
<script src="https://cdn.jsdelivr.net/npm/@grabzit/js@3.5.2/grabzit.min.js"></script>
<script>
GrabzIt("Sign in to view your Application Key").ConvertURL("https://www.bbc.com", 
{"format": "pdf", "download": 1, "target": "#Article", "height": -1, "width": -1}).Create();
</script>
var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");
var options = {"targetElement":"#Article","pageWidth":-1,"pageHeight":-1};
client.url_to_pdf("https://www.bbc.com", options);
client.save("https://www.example.com/handler", function (error, id){
    if (error != null){
        throw error;
    }
});
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret");
$options = GrabzItPDFOptions->new();
$options->targetElement("#Article");
$options->pageWidth(-1);
$options->pageHeight(-1);
$grabzIt->URLToPDF("https://www.bbc.com", $options);
$grabzIt->Save("https://www.mywebsite.com/handler.pl");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");
$options = new \GrabzIt\GrabzItPDFOptions();
$options->setTargetElement("#Article");
$options->setPageWidth(-1);
$options->setPageHeight(-1);
$grabzIt->URLToPDF("https://www.bbc.com", $options);
$grabzIt->Save("https://www.mywebsite.com/handler.php");
grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")
options = GrabzItPDFOptions.GrabzItPDFOptions()
options.targetElement = "#Article"
options.pageWidth = -1
options.pageHeight = -1
grabzIt.URLToPDF("https://www.bbc.com", options)
grabzIt.Save("https://www.mywebsite.com/handler.py")
https://api.grabz.it/convert?key=Sign in to view your Application Key&format=pdf&target=%23Article&height= -1&width=-1&url=https%3A%2F%2Fwww.bbc.com%2F
grabzIt = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret")
options = GrabzIt::PDFOptions.new()
options.targetElement = "#Article"
options.pageWidth = -1
options.pageHeigh = -1
grabzIt.url_to_pdf("https://www.bbc.com", options)
grabzIt.save("https://www.mywebsite.com/handler/index")