Skip to content

Commit

Permalink
Merge pull request #10 from Api2Pdf/2.0.0
Browse files Browse the repository at this point in the history
2.0.0
  • Loading branch information
apexdodge authored Jul 10, 2021
2 parents 5f7dd81 + e0548ae commit 3c741e2
Show file tree
Hide file tree
Showing 3 changed files with 284 additions and 146 deletions.
147 changes: 101 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# api2pdf.node
Nodejs client for [Api2Pdf REST API](https://www.api2pdf.com/documentation)
Nodejs client for [Api2Pdf REST API](https://www.api2pdf.com/documentation/v2)

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**.
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)
Expand Down Expand Up @@ -42,7 +42,7 @@ All usage starts by requiring api2pdf and creating a new object.
Once you initialize the client, you can make calls like so:

```
a2pClient.headlessChromeFromUrl('https://www.github.com')
a2pClient.chromeUrlToPdf('https://www.github.com')
.then(function(result) {
console.log(result); //successful api call
}, function(rejected) {
Expand All @@ -54,37 +54,36 @@ a2pClient.headlessChromeFromUrl('https://www.github.com')
### Successful Result Format

{
'pdf': 'https://link-to-pdf-only-available-for-24-hours',
'mbIn': 0.08421039581298828,
'mbOut': 0.08830547332763672,
'cost': 0.00017251586914062501,
'success': true,
'error': null,
'responseId': '6e46637a-650d-46d5-af0b-3d7831baccbb'
'FileUrl': 'https://link-to-file-only-available-for-24-hours',
'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'
'Success': false,
'Error': 'some reason for the error',
'ResponseId': '6e46637a-650d-46d5-af0b-3d7831baccbb'
}

## <a name="wkhtmltopdf"></a> wkhtmltopdf

**Convert HTML to PDF**

```
a2pClient.wkhtmltopdfFromHtml('<p>Hello, World</p>').then(function(result) {
a2pClient.wkHtmlToPdf('<p>Hello, World</p>').then(function(result) {
console.log(result);
});
```

**Convert HTML to PDF (load PDF in browser window and specify a file name)**
**Convert HTML to PDF (download PDF as a file and specify a file name)**

```
a2pClient.wkhtmltopdfFromHtml('<p>Hello, World</p>', inline = true, filename = 'test.pdf').then(function(result) {
a2pClient.wkHtmlToPdf('<p>Hello, World</p>', { inline: false, filename: 'test.pdf' }).then(function(result) {
console.log(result);
});
```
Expand All @@ -94,23 +93,23 @@ a2pClient.wkhtmltopdfFromHtml('<p>Hello, World</p>', inline = true, filename = '

```
var options = { orientation: 'landscape', pageSize: 'A4'};
a2pClient.wkhtmltopdfFromHtml('<p>Hello, World</p>', inline = true, filename = 'test.pdf', options = options).then(function(result) {
a2pClient.wkHtmlToPdf('<p>Hello, World</p>', { options: options }).then(function(result) {
console.log(result);
});
```

**Convert URL to PDF**

```
a2pClient.wkhtmltopdfFromUrl('https://www.github.com').then(function(result) {
a2pClient.wkUrlToPdf('https://www.github.com').then(function(result) {
console.log(result);
});
```

**Convert URL to PDF (load PDF in browser window and specify a file name)**
**Convert URL to PDF (download PDF as a file and specify a file name)**

```
a2pClient.wkhtmltopdfFromUrl('https://www.github.com', inline = true, filename = 'test.pdf').then(function(result) {
a2pClient.wkUrlToPdf('https://www.github.com', { inline: false, filename: 'test.pdf' }).then(function(result) {
console.log(result);
});
```
Expand All @@ -120,7 +119,7 @@ a2pClient.wkhtmltopdfFromUrl('https://www.github.com', inline = true, filename =

```
var options = { orientation: 'landscape', pageSize: 'A4'};
a2pClient.wkhtmltopdfFromUrl('https://www.github.com', inline = true, filename = 'test.pdf', options = options).then(function(result) {
a2pClient.wkUrlToPdf('https://www.github.com', { options: options }).then(function(result) {
console.log(result);
});
```
Expand All @@ -132,15 +131,15 @@ a2pClient.wkhtmltopdfFromUrl('https://www.github.com', inline = true, filename =
**Convert HTML to PDF**

```
a2pClient.headlessChromeFromHtml('<p>Hello, World</p>').then(function(result) {
a2pClient.chromeHtmlToPdf('<p>Hello, World</p>').then(function(result) {
console.log(result);
});
```

**Convert HTML to PDF (load PDF in browser window and specify a file name)**
**Convert HTML to PDF (download PDF as a file and specify a file name)**

```
a2pClient.headlessChromeFromHtml('<p>Hello, World</p>', inline = true, filename = 'test.pdf').then(function(result) {
a2pClient.chromeHtmlToPdf('<p>Hello, World</p>', { inline: false, filename: 'test.pdf' }).then(function(result) {
console.log(result);
});
```
Expand All @@ -150,23 +149,23 @@ a2pClient.headlessChromeFromHtml('<p>Hello, World</p>', inline = true, filename

```
var options = { landscape: true };
a2pClient.headlessChromeFromHtml('<p>Hello, World</p>', inline = true, filename = 'test.pdf', options = options).then(function(result) {
a2pClient.chromeHtmlToPdf('<p>Hello, World</p>', { options: options }).then(function(result) {
console.log(result);
});
```

**Convert URL to PDF**

```
a2pClient.headlessChromeFromUrl('https://www.github.com').then(function(result) {
a2pClient.chromeUrlToPdf('https://www.github.com').then(function(result) {
console.log(result);
});
```

**Convert URL to PDF (load PDF in browser window and specify a file name)**
**Convert URL to PDF (download PDF as a file and specify a file name)**

```
a2pClient.headlessChromeFromUrl('https://www.github.com', inline = true, filename = 'test.pdf').then(function(result) {
a2pClient.chromeUrlToPdf('https://www.github.com', { inline: false, filename: 'test.pdf' }).then(function(result) {
console.log(result);
});
```
Expand All @@ -176,57 +175,115 @@ a2pClient.headlessChromeFromUrl('https://www.github.com', inline = true, filenam

```
var options = { landscape: true };
a2pClient.headlessChromeFromUrl('https://www.github.com', inline = true, filename = 'test.pdf', options = options).then(function(result) {
a2pClient.chromeUrlToPdf('https://www.github.com', { options: options }).then(function(result) {
console.log(result);
});
```

**Convert HTML to Image**

```
a2pClient.chromeHtmlToImage('<p>Hello, World</p>').then(function(result) {
console.log(result);
});
```

**Convert URL to Image**

```
a2pClient.chromeUrlToImage('https://www.github.com').then(function(result) {
console.log(result);
});
```
---

## <a name="libreoffice"></a>LibreOffice

LibreOffice supports the conversion to PDF from the following file formats:
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.

- 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.
You must provide a url to the file. Our engine will consume the file at that URL and convert it.

**Convert Microsoft Office Document or Image to PDF**

```
a2pClient.libreofficeConvert('https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx').then(function(result) {
a2pClient.libreOfficeAnyToPdf('https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx').then(function(result) {
console.log(result);
});
```

**Convert Microsoft Office Document or Image to PDF (load PDF in browser window and specify a file name)**
**Convert Microsoft Office Document or Image to PDF (download PDF as a file and specify a file name)**

```
a2pClient.libreofficeConvert('https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx', { inline: false, filename: 'test.pdf' }).then(function(result) {
console.log(result);
});
```

**Thumbnail or Image Preview of a PDF or Office Document or Email file**

```
a2pClient.libreOfficeThumbnail('https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx').then(function(result) {
console.log(result);
});
```

**Convert HTML to Microsoft Word or Docx**

```
a2pClient.libreofficeConvert('https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx', inline = true, filename = 'test.pdf').then(function(result) {
a2pClient.libreOfficeHtmlToDocx('http://www.api2pdf.com/wp-content/uploads/2021/01/sampleHtml.html').then(function(result) {
console.log(result);
});
```

**Convert HTML to Microsoft Excel or Xlsx**

```
a2pClient.libreOfficeHtmlToXlsx('http://www.api2pdf.com/wp-content/uploads/2021/01/sampleTables.html').then(function(result) {
console.log(result);
});
```

**Convert PDF to HTML**

```
a2pClient.libreOfficePdfToHtml('http://www.api2pdf.com/wp-content/uploads/2021/01/1a082b03-2bd6-4703-989d-0443a88e3b0f-4.pdf').then(function(result) {
console.log(result);
});
```

---

## <a name="merge"></a>Merge / Concatenate Two or More PDFs
## <a name="merge"></a>PdfSharp - Merge / Concatenate Two or More PDFs, Add bookmarks to pdfs, add passwords to 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**

```
var urls = ['url-to-pdf1', 'url-to-pdf2'];
a2pClient.merge(urls).then(function(result) {
a2pClient.pdfsharpMerge(urls).then(function(result) {
console.log(result);
});
```

**Merge PDFs from list of URLs to existing PDFs (load PDF in browser window and specify a file name)**
**Add bookmarks to existing PDF**

```
var urls = ['url-to-pdf1', 'url-to-pdf2'];
a2pClient.merge(urls, inline = true, filename = 'test.pdf').then(function(result) {
var url = 'https://LINK-TO-PDF';
var bookmarks = [
{ Page: 0, Title: "Introduction"},
{ Page: 1, Title: "Second page"}
]
a2pClient.pdfsharpAddBookmarks(url, bookmarks).then(function(result) {
console.log(result);
});
```

**Add password to existing PDF**
```
var url = 'https://LINK-TO-PDF';
var userpassword = "hello";
a2pClient.pdfsharpAddPassword(url, userpassword).then(function(result) {
console.log(result);
});
```
Expand All @@ -236,11 +293,9 @@ a2pClient.merge(urls, inline = true, filename = 'test.pdf').then(function(result

**Delete a PDF on Command with delete(responseId)**

By default, Api2Pdf will automatically delete your PDFs after 24 hours. If you have higher security requirements and need to delete the PDFs at-will, you can do so by calling the `delete(responseId)` method on the Api2Pdf object where `responseId` parameter comes from the responseId attribute in the result.
By default, Api2Pdf will delete your generated file 24 hours after it has been generated. For those with high security needs, you may want to delete your file on command. You can do so by making an DELETE api call with the `responseId` attribute that was returned on the original JSON payload.

```
a2pClient.headlessChromeFromHtml('<p>Hello, World</p>').then(function(result) {
console.log(result);
a2pClient.delete(result.responseId); //delete pdf by using responseId attribute
});
var responseId = result.ResponseId //from previous api call
a2pClient.utilityDelete(responseId);
```
Loading

0 comments on commit 3c741e2

Please sign in to comment.