Podczas konwersji stron internetowych i HTML na PDF GrabzIt's Node.js API zapewnia następujące funkcje, które pomagają integrate GrabzIt into twój system tak łatwo, jak to możliwe. Jednak zanim zaczniesz, pamiętaj, że po wywołaniu url_to_pdf, html_to_pdf or file_to_pdf metody save or save_to należy wywołać metodę, aby wykonać zrzut ekranu PDF lub przekonwertować HTML bezpośrednio na format PDF.
Do konwersji strony internetowej wymagany jest tylko jeden parametr into dokument PDF lub do konwertuj HTML na PDF jak pokazano poniżej.
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
Możesz przekazać niestandardowy identyfikator do PDF metody pokazane poniżej, ta wartość jest następnie zwracana do modułu obsługi GrabzIt Node.js. Na przykład ten niestandardowy identyfikator może być identyfikatorem bazy danych, umożliwiając skojarzenie zrzutu ekranu z określonym rekordem bazy danych.
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; } });
Podczas tworzenia zrzutu ekranu w formacie PDF możesz poprosić o zastosowanie określonego szablon do wygenerowanego pliku PDF. Ten szablon musi być saved z góry i określi zawartość nagłówka i stopki wraz ze specjalnymi zmiennymi. W przykładowym kodzie poniżej użytkownik używa swojego szablonu o nazwie „mój szablon”.
Jeśli nie ma wystarczająco dużego górnego lub dolnego marginesu odpowiednio dla nagłówka lub stopki, nie pojawi się on w pliku PDF. W poniższym przykładzie ustawiliśmy górny i dolny margines na 20, aby zapewnić dużo miejsca.
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; } });
Jeśli chcesz bezpośrednio przekonwertować element HTML, taki jak div lub span, bezpośrednio into dokument PDF można z biblioteką Node.js GrabzIt. Musisz zdać Selektor CSS elementu HTML, który chcesz przekonwertować na targetElement
parametr.
... <span id="Article"> <p>This is the content I am interested in.</p> <img src="myimage.jpg"> </span> ...
W tym przykładzie chcemy uchwycić całą zawartość w zakresie, który ma identyfikator Article
, dlatego przekazujemy to do GrabzIt, jak pokazano poniżej.
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; } });
Sposób przycinania pliku PDF podczas kierowania elementu HTML może być kontrolowane przy użyciu tych technik.