將網頁和HTML轉換為PDF時 GrabzIt的Node.js API 提供以下有助於幫助的功能 integrate GrabzIt into您的系統盡可能地容易。 但是,在開始之前,請記住 url_to_pdf, html_to_pdf or file_to_pdf 方法 save or save_to 必須調用方法來獲取PDF屏幕截圖或將HTML直接轉換為PDF。
只需一個參數即可轉換網頁 intpdf文檔或 將HTML轉換為PDF 如下圖所示。
client.url_to_pdf("https://www.tesla.com"); //Then call the save or save_to method
client.html_to_pdf("<html><body><h1>Hello World!</h1></body></html>"); //Then call the save or save_to method
client.file_to_pdf("example.html"); //Then call the save or save_to method
您可以將自定義標識符傳遞給 PDF 方法,如下所示,然後將此值返回到GrabzIt Node.js處理程序。 例如,該自定義標識符可以是數據庫標識符,從而允許將屏幕截圖與特定數據庫記錄相關聯。
var grabzit = require('grabzit'); var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret"); var options = {"customId":123456}; client.url_to_pdf("https://www.tesla.com", options); //Then call the save method client.save("http://www.example.com/handler", function (error, id){ if (error != null){ throw error; } });
var grabzit = require('grabzit'); var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret"); var options = {"customId":123456}; client.html_to_pdf("<html><body><h1>Hello World!</h1></body></html>", options); //Then call the save method client.save("http://www.example.com/handler", function (error, id){ if (error != null){ throw error; } });
var grabzit = require('grabzit'); var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret"); var options = {"customId":123456}; client.file_to_pdf("example.html", options); //Then call the save method client.save("http://www.example.com/handler", function (error, id){ if (error != null){ throw error; } });
創建PDF屏幕截圖時,您可以要求您應用特定的 模板 到生成的PDF。 該模板必須是 saved並會指定頁眉和頁腳的內容以及任何特殊變量。 在下面的示例代碼中,用戶正在使用其名為“我的模板”的模板。
如果頁眉或頁腳分別沒有足夠大的頂部或底部邊距,則它將不會出現在PDF中。 在下面的示例中,我們將頂部和底部邊距設置為20以提供足夠的空間。
var grabzit = require('grabzit'); var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret"); var options = {"marginTop":20,"marginBottom":20,"templateId":"my template"}; client.url_to_pdf("https://www.tesla.com", options); //Then call the save or save_to method client.save_to("result.pdf", function (error, id){ //this callback is called once the capture is downloaded if (error != null){ throw error; } });
var grabzit = require('grabzit'); var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret"); var options = {"marginTop":20,"marginBottom":20,"templateId":"my template"}; client.html_to_pdf("<html><body><h1>Hello World!</h1></body></html>", options); //Then call the save or save_to method client.save_to("result.pdf", function (error, id){ //this callback is called once the capture is downloaded if (error != null){ throw error; } });
var grabzit = require('grabzit'); var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret"); var options = {"marginTop":20,"marginBottom":20,"templateId":"my template"}; client.file_to_pdf("example.html", options); //Then call the save or save_to method client.save_to("result.pdf", function (error, id){ //this callback is called once the capture is downloaded if (error != null){ throw error; } });
如果只想直接轉換div或span等HTML元素 int您可以使用GrabzIt的Node.js庫獲得PDF文檔。 您必須通過 CSS選擇器 您希望轉換為HTML元素的 targetElement
參數。
... <span id="Article"> <p>This is the content I am interested in.</p> <img src="myimage.jpg"> </span> ...
在此示例中,我們希望捕獲跨度中ID為的所有內容 Article
,因此我們將其傳遞給GrabzIt,如下所示。
var grabzit = require('grabzit'); var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret"); grabzIt.url_to_pdf("http://www.bbc.co.uk/news", {"targetElement":"#Article"}); //Then call the save or save_to method client.save_to("result.pdf", function (error, id){ //this callback is called once the capture is downloaded if (error != null){ throw error; } });
定位HTML元素時如何裁剪PDF 使用這些技術進行控制.