Salesforce Apex code for Api2Pdf REST API
Api2Pdf.com is a REST API for instantly generating PDF documents from HTML, URLs, Microsoft Office Documents (Word, Excel, PPT), and images. The API also supports merge / concatenation of two or more PDFs. Api2Pdf is a wrapper for popular libraries such as wkhtmltopdf, Headless Chrome, and LibreOffice.
-
Click here (https://login.salesforce.com/packaging/installPackage.apexp?p0=04t4J0000005401)
-
Login with your Salesforce Credentials
-
Install the Unmanaged Package OR copy and paste all of the code from this repo
-
Once the apex files are added, go to your Visual Force pages and find the page for api2pdf_settings
-
Create an account at portal.api2pdf.com to get your API key and enter it into the page
-
You are ready to use api2pdf classes, for usage you can see examples here: https://github.com/Api2Pdf/api2pdf.salesforce/blob/master/Api2PdfController.cls
-
If you wish to test this out, take a look at the api2pdf_page visual force page
Resources this API supports:
Create an account at portal.api2pdf.com to get your API key.
All usage starts by calling the import command and initializing the client by passing your API key as a parameter to the constructor.
Api2PdfClient a2pClient = new Api2PdfClient("your-api-key");
Once you initialize the client, you can make calls like so:
Api2PdfResponse response = a2pClient.wkhtmlToPdfFromHtml('<p>test</p>', true, 'test.pdf');
String PdfUrl = response.getPdf();
An ApiResult object is returned from every API call. If a call is unsuccessful then an exception will be thrown with a message containing the result of failure.
Additional attributes include the total data usage in, out, and the cost for the API call, typically very small fractions of a penny.
private String responseId;
private String error;
private String mbOut;
private String pdf;
private Boolean success;
private String cost;
private String mbIn;
Convert HTML to PDF
Api2PdfResponse pdfResponse = a2pClient.wkhtmlToPdfFromHtml('<p>test</p>', true, 'test.pdf');
String pdfUrl = pdfResponse.getPdf();
Convert HTML to PDF (use arguments for advanced wkhtmltopdf settings) View full list of wkhtmltopdf options available.
Map<String, String> options = new Map<String, String>();
options.put('orientation', 'landscape');
options.put('pageSize', 'A4');
Api2PdfResponse pdfResponse = a2pClient.wkhtmlToPdfFromHtml('<p>test</p>', true, 'test.pdf', options);
String pdfUrl = pdfResponse.getPdf();
Convert URL to PDF
Api2PdfResponse pdfResponse = a2pClient.wkhtmlToPdfFromUrl('https://www.google.com', true, 'test.pdf');
String pdfUrl = pdfResponse.getPdf();
Convert URL to PDF (use arguments for advanced wkhtmltopdf settings) View full list of wkhtmltopdf options available.
Map<String, String> options = new Map<String, String>();
options.put('orientation', 'landscape');
options.put('pageSize', 'A4');
Api2PdfResponse pdfResponse = a2pClient.wkhtmlToPdfFromUrl('https://www.google.com', true, 'test.pdf', options);
String pdfUrl = pdfResponse.getPdf();
Convert HTML to PDF
Api2PdfResponse response = a2pClient.headlessChromeFromHtml('<p>test</p>', true, 'test.pdf');
String pdfUrl = response.getPdf();
Convert HTML to PDF (use arguments for advanced Headless Chrome settings) View full list of Headless Chrome options available.
Map<String, String> options = new Map<String, String>();
options.put('landscape', 'true');
Api2PdfResponse pdfResponse = a2pClient.headlessChromeFromHtml('<p>test</p>', true, 'test.pdf', options);
String pdfUrl = pdfResponse.getPdf();
Convert URL to PDF
Api2PdfResponse pdfResponse = a2pClient.headlessChromeFromUrl('https://www.google.com', true, 'test.pdf');
String pdfUrl = pdfResponse.getPdf();
Convert URL to PDF (use arguments for advanced Headless Chrome settings) View full list of Headless Chrome options available.
Map<String, String> options = new Map<String, String>();
options.put('landscape', 'true');
Api2PdfResponse pdfResponse = a2pClient.headlessChromeFromUrl('https://www.google.com', true, 'test.pdf', options);
String pdfUrl = pdfResponse.getPdf();
LibreOffice supports the conversion to PDF from the following file formats:
- doc / docx
- xls / xlsx
- ppt / pptx
- gif
- jpg
- png
- bmp
- rtf
- txt
- html
You must provide a URL to the file. Our engine will consume the file at that URL and convert it to the PDF.
Convert Microsoft Office Document or Image to PDF
Api2PdfResponse pdfResponse = a2pClient.libreofficeConvert('http://homepages.inf.ed.ac.uk/neilb/TestWordDoc.doc', true, 'test.pdf');
String pdfUrl = pdfResponse.getPdf();
To use the merge endpoint, supply a list of URLs to existing PDFs. The engine will consume all of the PDFs and merge them into a single PDF, in the order in which they were provided in the list.
Merge PDFs from list of URLs to existing PDFs
String[] urls = new List<String>();
urls.add('http://www.orimi.com/pdf-test.pdf');
urls.add('http://www.orimi.com/pdf-test.pdf');
Api2PdfResponse pdfResponse = a2pClient.mergePdf(urls, true, 'test.pdf');
String pdfUrl = pdfResponse.getPdf();