https://github.com/api2pdf/api2pdf.java
Java client library for the Api2Pdf.com REST API - Convert HTML to PDF, URL to PDF, Office Docs to PDF, Merge PDFs, HTML to Image, URL to Image, HTML to Docx, HTML to Xlsx, PDF to HTML, Thumbnail preview of office files
https://github.com/api2pdf/api2pdf.java
aws-lambda barcode headless-chrome html-to-docx html-to-image html-to-pdf html-to-xlsx image-preview java libreoffice merge-pdf pdf pdf-generation pdf-to-html qrcode rest-api url-to-image url-to-pdf wkhtmltopdf word-to-pdf
Last synced: 6 months ago
JSON representation
Java client library for the Api2Pdf.com REST API - Convert HTML to PDF, URL to PDF, Office Docs to PDF, Merge PDFs, HTML to Image, URL to Image, HTML to Docx, HTML to Xlsx, PDF to HTML, Thumbnail preview of office files
- Host: GitHub
- URL: https://github.com/api2pdf/api2pdf.java
- Owner: Api2Pdf
- License: mit
- Created: 2019-01-06T17:20:48.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2022-10-03T16:12:19.000Z (over 3 years ago)
- Last Synced: 2023-03-06T22:49:25.198Z (about 3 years ago)
- Topics: aws-lambda, barcode, headless-chrome, html-to-docx, html-to-image, html-to-pdf, html-to-xlsx, image-preview, java, libreoffice, merge-pdf, pdf, pdf-generation, pdf-to-html, qrcode, rest-api, url-to-image, url-to-pdf, wkhtmltopdf, word-to-pdf
- Language: Java
- Homepage: https://www.api2pdf.com
- Size: 34.2 KB
- Stars: 6
- Watchers: 4
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://opensource.org/licenses/MIT)
[](https://maven-badges.herokuapp.com/maven-central/com.api2pdf.client/api2pdf)
# api2pdf.java
Java client for [Api2Pdf REST API](https://www.api2pdf.com/documentation/v2)
Api2Pdf.com is a powerful REST API for instantly generating PDF and Office documents from HTML, URLs, Microsoft Office Documents (Word, Excel, PPT), Email files, and images. You can generate image preview or thumbnail of a PDF, office document, or email file. The API also supports merge / concatenation of two or more PDFs, setting passwords on PDFs, and adding bookmarks to PDFs. Api2Pdf is a wrapper for popular libraries such as **wkhtmltopdf**, **Headless Chrome**, **PdfSharp**, and **LibreOffice**.
- [Installation](#installation)
- [Resources](#resources)
- [Authorization](#authorization)
- [Usage](#usage)
- [FAQ](https://www.api2pdf.com/faq)
The Java client library is available as a Maven Package and can be installed with Maven by including the following dependency in your pom.xml file.
com.api2pdf.client
api2pdf
2.0.0
Resources this API supports:
- [wkhtmltopdf](#wkhtmltopdf)
- [Headless Chrome](#chrome)
- [LibreOffice](#libreoffice)
- [Merge / Concatenate PDFs](#merge)
### Acquire API Key
Create an account at [portal.api2pdf.com](https://portal.api2pdf.com/register) to get your API key.
## Usage
### Initialize the Client
All usage starts by importing the api2pdf library and creating a new object.
package com.api2pdf.client;
import com.api2pdf.models.Api2PdfResponse;
Api2PdfClient a2pClient = new Api2PdfClient('YOUR-API-KEY');
Once you initialize the client, you can make calls like so:
```
Api2PdfResponse response = a2pClient.chromeHtmlToPdf("
test
", true, "test.pdf");
System.out.println(response.getFile());
```
### Successful Result Format
{
'FileUrl': 'https://link-to-pdf-only-available-for-24-hours',
'Seconds': 0.08421039581298828,
'MbOut': 0.08830547332763672,
'Cost': 0.00017251586914062501,
'Success': true,
'Error': null,
'ResponseId': '6e46637a-650d-46d5-af0b-3d7831baccbb'
}
### Failed Result Format
{
'Success': false,
'Error': 'some reason for the error',
'ResponseId': '6e46637a-650d-46d5-af0b-3d7831baccbb'
}
## wkhtmltopdf
**Convert HTML to PDF (load PDF in browser window (true or false) and specify a file name)**.
```
Api2PdfResponse response = a2pClient.wkhtmlHtmlToPdf("
Hello, World
", true, "test.pdf");
System.out.println(response.getFile());
```
**Convert HTML to PDF (use HashMap for advanced wkhtmltopdf settings)**
[View full list of wkhtmltopdf options available.](https://www.api2pdf.com/documentation/advanced-options-wkhtmltopdf/)
```
Map options = new HashMap();
options.put("orientation", "landscape");
options.put("pageSize", "A4");
Api2PdfResponse response = a2pClient.wkhtmlHtmlToPdf("
Hello, World
", true, "test.pdf", options);
System.out.println(response.getFile());
```
**Convert URL to PDF (load PDF in browser window (true or false) and specify a file name)**.
```
Api2PdfResponse response = a2pClient.wkhtmlUrlToPdf("https://www.github.com", true, "test.pdf");
System.out.println(response.getFile());
```
**Convert URL to PDF (use HashMap for advanced wkhtmltopdf settings)**
[View full list of wkhtmltopdf options available.](https://www.api2pdf.com/documentation/advanced-options-wkhtmltopdf/)
```
Map options = new HashMap();
options.put("orientation", "landscape");
options.put("pageSize", "A4");
Api2PdfResponse response = a2pClient.wkhtmlUrlToPdf("https://www.github.com", true, "test.pdf", options);
System.out.println(response.getFile());
```
---
**Convert HTML to PDF (load PDF in browser window (true or false) and specify a file name)**
```
Api2PdfResponse response = a2pClient.chromeHtmlToPdf("
Hello World
", true, "test.pdf");
System.out.println(response.getFile());
```
**Convert HTML to PDF (use HashMap for advanced Headless Chrome settings)**
[View full list of Headless Chrome options available.](https://www.api2pdf.com/documentation/advanced-options-headless-chrome/)
```
Map options = new HashMap();
options.put("landscape", "true");
Api2PdfResponse response = a2pClient.chromeHtmlToPdf("
Hello World
", true, "test.pdf", options);
System.out.println(response.getFile());
```
**Convert URL to PDF (load PDF in browser window (true or false) and specify a file name)**
```
Api2PdfResponse response = a2pClient.chromeUrlToPdf("https://www.github.com", true, "test.pdf");
System.out.println(response.getFile());
```
**Convert URL to PDF (use HashMap for advanced Headless Chrome settings)**
[View full list of Headless Chrome options available.](https://www.api2pdf.com/documentation/advanced-options-headless-chrome/)
```
Map options = new HashMap();
options.put("landscape", "true");
Api2PdfResponse response = a2pClient.chromeUrlToPdf("https://www.github.com", true, "test.pdf", options);
System.out.println(response.getFile());
```
**Convert HTML to Image (load PDF in browser window (true or false) and specify a file name)**
```
Api2PdfResponse response = a2pClient.chromeHtmlToImage("
Hello World
", true, "test.png");
System.out.println(response.getFile());
```
**Convert URL to Image (load PDF in browser window (true or false) and specify a file name)**
```
Api2PdfResponse response = a2pClient.chromeUrlToImage("https://www.github.com", true, "test.png");
System.out.println(response.getFile());
```
---
Convert any office file to PDF, image file to PDF, email file to PDF, HTML to Word, HTML to Excel, and PDF to HTML. Any file that can be reasonably opened by LibreOffice should be convertible. Additionally, we have an endpoint for generating a *thumbnail* of the first page of your PDF or Office Document. This is great for generating an image preview of your files to users.
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 (load PDF in browser window (true or false) and specify a file name)**
```
Api2PdfResponse response = a2pClient.libreofficeAnyToPdf("https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx", true, "test.pdf");
System.out.println(response.getFile());
```
**Thumbnail or Image Preview of a PDF or Office Document or Email file**
```
Api2PdfResponse response = a2pClient.libreofficeThumbnail("https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx", true, "test.png");
System.out.println(response.getFile());
```
**Convert HTML to Microsoft Word or Docx**
```
Api2PdfResponse response = a2pClient.libreofficeHtmlToDocx("http://www.api2pdf.com/wp-content/uploads/2021/01/sampleHtml.html", true, "test.png");
System.out.println(response.getFile());
```
**Convert HTML to Microsoft Excel or Xlsx**
```
Api2PdfResponse response = a2pClient.libreofficeHtmlToDocx("http://www.api2pdf.com/wp-content/uploads/2021/01/sampleTables.html", true, "test.docx");
System.out.println(response.getFile());
```
**Convert HTML to Microsoft Excel or Xlsx**
```
Api2PdfResponse response = a2pClient.libreofficeHtmlToXlsx("http://www.api2pdf.com/wp-content/uploads/2021/01/sampleTables.html", true, "test.xlsx");
System.out.println(response.getFile());
```
**Convert PDF to HTML**
```
Api2PdfResponse response = a2pClient.libreofficeHtmlToXlsx("http://www.api2pdf.com/wp-content/uploads/2021/01/1a082b03-2bd6-4703-989d-0443a88e3b0f-4.pdf", true, "test.png");
System.out.println(response.getFile());
```
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 array of URLs to existing PDFs (load PDF in browser window (true or false) and specify a file name)****
```
String[] urls = { "http://www.api2pdf.com/wp-content/uploads/2021/01/1a082b03-2bd6-4703-989d-0443a88e3b0f-4.pdf", "http://www.api2pdf.com/wp-content/uploads/2021/01/1a082b03-2bd6-4703-989d-0443a88e3b0f-4.pdf" };
Api2PdfResponse response = a2pClient.pdfsharpMerge(urls, true, "test.pdf");
System.out.println(response.getFile());
```
**Add PDF bookmarks to an existing PDF**
```
import com.api2pdf.models.Api2PdfBookmarkItemModel;
String url = "http://www.api2pdf.com/wp-content/uploads/2021/01/1a082b03-2bd6-4703-989d-0443a88e3b0f-4.pdf";
Api2PdfBookmarkItemModel[] bookmarks = new Api2PdfBookmarkItemModel[] { new Api2PdfBookmarkItemModel(0, "Title page") };
Api2PdfResponse response = a2pClient.pdfsharpAddBookmarks(url, bookmarks, true, "test.pdf");
System.out.println(response.getFile());
```
**Add password to existing PDF**
```
String url = "http://www.api2pdf.com/wp-content/uploads/2021/01/1a082b03-2bd6-4703-989d-0443a88e3b0f-4.pdf";
String password = "hello";
Api2PdfResponse response = a2pClient.pdfsharpAddBookmarks(url, password, true, "test.pdf");
System.out.println(response.getFile());
```