捕獲和轉換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")