Skip to content
Salesforce Apex client code for converting HTML to PDF, URL to PDF, Office files to PDF, and Merging PDFs together
Apex
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
models
tests
visualforce pages
Api2PdfClient.cls
Api2PdfController.cls
Api2PdfSettingsController.cls
LICENSE
README.md

README.md

api2pdf for Salesforce

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.

Installation

  1. Click here (https://login.salesforce.com/packaging/installPackage.apexp?p0=04t4J0000005401)

  2. Login with your Salesforce Credentials

  3. Install the Unmanaged Package OR copy and paste all of the code from this repo

  4. Once the apex files are added, go to your Visual Force pages and find the page for api2pdf_settings image

  5. Click the icon to load the page image

  6. Create an account at portal.api2pdf.com to get your API key and enter it into the page

  7. You are ready to use api2pdf classes, for usage you can see examples here: https://github.com/Api2Pdf/api2pdf.salesforce/blob/master/Api2PdfController.cls

  8. If you wish to test this out, take a look at the api2pdf_page visual force page

Resources

Resources this API supports:

Authorization

Acquire API Key

Create an account at portal.api2pdf.com to get your API key.

Usage

Initialize the Client

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();

Result Format

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;

wkhtmltopdf

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();

Headless Chrome

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

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();

Merge / Concatenate Two or More PDFs

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();
You can’t perform that action at this time.