捕获和转换Web的工具

如何控制PDF内容的裁剪?

在PDF转换中将HTML元素作为目标时,结果将被裁剪为仅显示HTML元素。 但是,问题仍然在于如何处理其余页面。 应该显示整个页面,目标HTML元素位于顶部还是仅HTML元素?

默认情况下,将显示整个页面,并在页面顶部显示目标HTML元素。 如右例所示。

但是,有时需要生成PDF,该PDF仅显示所选的HTML元素,而不显示页面的其余部分。 左侧显示了一个示例。

为此,您必须将-1传递给页面的width和height参数。 但是不幸的是,这种方法更加复杂,因为必须在裁剪PDF之前将其渲染。

因此,如果默认页面大小不足以很好地呈现HTML元素,则可能需要设置更大的页面大小和/或方向,以确保按要求正确呈现。

自动将页面大小裁剪为HTML目标

请注意,自动调整页面大小功能将起作用 只能 指定HTML元素时。

GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");
PDFOptions options = new PDFOptions();
options.TargetElement = "#Article";
options.PageWidth = -1;
options.PageHeight = -1;
grabzIt.URLToPDF("http://www.spacex.com", options);
grabzIt.Save("http://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("http://www.spacex.com", options);
grabzIt.Save("http://www.mywebsite.com/handler");
<script src="https://cdn.jsdelivr.net/npm/@grabzit/js@3.4.7/grabzit.min.js"></script>
<script>
GrabzIt("Sign in to view your Application Key").ConvertURL("http://www.spacex.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("http://www.spacex.com", options);
client.save("http://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("http://www.spacex.com", $options);
$grabzIt->Save("http://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("http://www.spacex.com", $options);
$grabzIt->Save("http://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("http://www.spacex.com", options)
grabzIt.Save("http://www.mywebsite.com/handler.py")
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("http://www.spacex.com", options)
grabzIt.save("http://www.mywebsite.com/handler/index")