将网页和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 使用这些技术进行控制.