Ao converter páginas da web e HTML para PDF API Node.js do GrabzIt fornece os seguintes recursos que ajudam integrate GrabzIt into seu sistema o mais facilmente possível. No entanto, antes de começar, lembre-se de que depois de ligar para o url_to_pdf, html_to_pdf or file_to_pdf métodos os save or save_to O método deve ser chamado para obter a captura de tela do PDF ou converter o HTML diretamente em PDF.
Apenas um parâmetro é necessário para converter uma página da web intum documento PDF ou para converter HTML para PDF como mostrado abaixo.
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
Você pode passar um identificador personalizado para o PDF Como mostrado abaixo, esse valor é retornado ao seu manipulador GrabzIt Node.js. Por exemplo, esse identificador personalizado pode ser um identificador de banco de dados, permitindo que uma captura de tela seja associada a um registro específico do banco de dados.
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; } });
Ao criar uma captura de tela em PDF, você pode solicitar a aplicação de um determinado modelo para o PDF gerado. Este modelo deve ser saved com antecedência e especificará o conteúdo do cabeçalho e rodapé, juntamente com quaisquer variáveis especiais. No código de exemplo abaixo, o usuário está usando o modelo chamado "meu modelo".
Se não houver uma margem superior ou inferior suficientemente grande para o cabeçalho ou rodapé, respectivamente, ela não aparecerá no PDF. No exemplo abaixo, definimos as margens superior e inferior como 20 para fornecer bastante espaço.
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; } });
Se você deseja apenas converter um elemento HTML, como um div ou span, diretamente intum documento PDF que você pode com a biblioteca Node.js. do GrabzIt. Você deve passar o Seletor CSS do elemento HTML que você deseja converter para o targetElement
parâmetro.
... <span id="Article"> <p>This is the content I am interested in.</p> <img src="myimage.jpg"> </span> ...
Neste exemplo, queremos capturar todo o conteúdo no período que possui o ID de Article
, portanto, passamos isso para o GrabzIt, como mostrado abaixo.
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; } });
Como um PDF é cortado ao segmentar um elemento HTML pode ser controlado usando essas técnicas.