GithubHelp home page GithubHelp logo

api2pdf / api2pdf.java Goto Github PK

View Code? Open in Web Editor NEW
7.0 4.0 8.0 35 KB

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

Home Page: https://www.api2pdf.com

License: MIT License

Java 100.00%
pdf pdf-generation html-to-pdf url-to-pdf merge-pdf word-to-pdf wkhtmltopdf headless-chrome libreoffice aws-lambda java rest-api barcode qrcode html-to-docx html-to-xlsx image-preview pdf-to-html html-to-image url-to-image

api2pdf.java's Introduction

License: MIT Maven Central

api2pdf.java

Java client for Api2Pdf REST API

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

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.

<dependencies>
    <dependency>
        <groupId>com.api2pdf.client</groupId>
        <artifactId>api2pdf</artifactId>
        <version>2.0.0</version>
    </dependency>
</dependencies>

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 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("<p>test</p>", 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("<p>Hello, World</p>", 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.

Map<String, String> options = new HashMap<String, String>();
options.put("orientation", "landscape");
options.put("pageSize", "A4");
Api2PdfResponse response = a2pClient.wkhtmlHtmlToPdf("<p>Hello, World</p>", 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.

Map<String, String> options = new HashMap<String, String>();
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());

Headless Chrome

Convert HTML to PDF (load PDF in browser window (true or false) and specify a file name)

Api2PdfResponse response = a2pClient.chromeHtmlToPdf("<p>Hello World</p>", 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.

Map<String, String> options = new HashMap<String, String>();
options.put("landscape", "true");
Api2PdfResponse response = a2pClient.chromeHtmlToPdf("<p>Hello World</p>", 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.

Map<String, String> options = new HashMap<String, String>();
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("<p>Hello World</p>", 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());

LibreOffice

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

PdfSharp

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

api2pdf.java's People

Contributors

apexdodge avatar husseinmosaad avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

api2pdf.java's Issues

support utf-8 charset by default

Reports that using meta charset="UTF-8" in head of html is ignored while using the java client library. Annoying.

This is the reported fix to the makeRequest function:

image

Single Quotes vs Double Quotes in /chrome/html endpoint

Hi,

I've started using api2pdf via this library and one thing that happened to me was that I was getting a 400 http response from the endpoint, and couldn't find out what was wrong (I was kind of expecting the error field in the response to help me out).

I was was calling the following api:
Api2PdfResponse pdfResponse = a2pClient.headlessChromeFromHtml("the html content", false, "file.pdf", options);

After digging through the source code of the library I saw that you guys build the JSON input for the api as the following:

String payload = "{\"html\":\"" + html + "\",\"inlinePdf\":" + inlinePdf + ",\"fileName\":\"" + fileName
				+ "\", \"options\":{ " + optionsPayload + "}}";

Which means that any double quotes on the html variable would probably mess things up and sure enough, once I changed all double quotes to single quotes, things started working.

One thing that was kind frustrating to find out was that after this I added <meta charset="utf-8" /> (notice the double quotes) and had weird errors when using things like º (displayed as ). Once I put the single quotes, everything was ok. (This issue with the encoding also happended when using the swagger api documentation)

Downloading a resource (403 forbidden)

Hi,

This isn't really an issue with the api2pdf java API, but, I think you guys might be able to help.

After successfully invoking the api and receiving a response I have the following:

Api2PdfResponse pdfResponse =
          a2pClient.headlessChromeFromHtml(baos.toString("UTF-8"), false, "wps.pdf", options);
      String pdfUrl = pdfResponse.getPdf();

I want to download the file in pdfUrl but I keep getting a 403 forbidden.
I've tried, URLDecoding the value (and not decoding) the URL. I've tried using apache.commons.FileUtils.copyInputStreamToFile(); and i've tried making my own download (using URLConnection) but I always get the 403 return code.

I've printout the pdfUrl and copy it to the address bar of my browser, it works perfectly. What header/cookie am I missing in the java side to be able to download the file?

Thanks

a2pClient.headlessChromeFromHtml does not accept backslashes

Api2PdfClient a2pClient = new Api2PdfClient(api2PDFProperties.getApiKey());

String html = "String with backslash \ "

//below code breaks with a 400 error
String pdfURL = a2pClient
.headlessChromeFromHtml(html, true, filename, options)
.getPdf();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.