Coder Social home page Coder Social logo

yuesir / api2pdf.dotnet Goto Github PK

View Code? Open in Web Editor NEW

This project forked from api2pdf/api2pdf.dotnet

0.0 2.0 0.0 23 KB

C# client library for the Api2Pdf.com REST API - Convert HTML to PDF, URL to PDF, Office Docs to PDF, Merge PDFs with AWS Lambda

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

License: MIT License

C# 100.00%

api2pdf.dotnet's Introduction

api2pdf.dotnet

.NET bindings 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.

Add a dependency

nuget

Run the nuget command for installing the client Install-Package Api2Pdf

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 initializing the client by passing your API key as a parameter to the constructor.

var a2pClient = new Api2Pdf("YOUR-API-KEY");

Once you initialize the client, you can make calls like so:

var apiResponse = a2pClient.HeadlessChrome.FromHtml("<p>Hello, World</p>");
Console.WriteLine(apiResponse.Pdf);
Console.ReadLine();

Result Object

An Api2PdfResponse object is returned from every API call. If a call is unsuccessful then success will show False and the error will provide the reason for failure. Additional attributes include the total data usage in, out, and the cost for the API call, typically very small fractions of a penny.

{
    'pdf': 'https://link-to-pdf-only-available-for-24-hours',
    'mbIn': 0.08421039581298828,
    'mbOut': 0.08830547332763672,
    'cost': 0.00017251586914062501,
    'success': True,
    'error': None,
    'responseId': '6e46637a-650d-46d5-af0b-3d7831baccbb'
}

wkhtmltopdf

Convert HTML to PDF

var apiResponse = a2pClient.WkHtmlToPdf.FromHtml("<p>Hello, World</p>");

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

var apiResponse = a2pClient.WkHtmlToPdf.FromHtml("<p>Hello, World</p>", inline: true, outputFileName: "test.pdf");

Convert HTML to PDF (use dictionary for advanced wkhtmltopdf settings) View full list of wkhtmltopdf options available.

var options = new Dictionary<string, string>();
options.Add("orientation", "landscape");
options.Add("pageSize", "Letter");
var apiResponse = a2pClient.WkHtmlToPdf.FromHtml("<p>Hello, World</p>", options: options);

Convert URL to PDF

var apiResponse = a2pClient.WkHtmlToPdf.FromUrl("http://www.api2pdf.com");

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

var apiResponse = a2pClient.WkHtmlToPdf.FromUrl("http://www.api2pdf.com", inline: true, outputFileName: "test.pdf");

Convert URL to PDF (use dictionary for advanced wkhtmltopdf settings) View full list of wkhtmltopdf options available.

var options = new Dictionary<string, string>();
options.Add("orientation", "landscape");
options.Add("pageSize", "Letter");
var apiResponse = a2pClient.WkHtmlToPdf.FromUrl("http://www.api2pdf.com", options: options);

Headless Chrome

Convert HTML to PDF

var apiResponse = a2pClient.HeadlessChrome.FromHtml("<p>Hello, World</p>");

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

var apiResponse = a2pClient.HeadlessChrome.FromHtml("<p>Hello, World</p>", inline: true, outputFileName: "test.pdf");

Convert HTML to PDF (use dictionary for advanced Headless Chrome settings) View full list of Headless Chrome options available.

var options = new Dictionary<string, string>();
options.Add("landscape", "true");
var apiResponse = a2pClient.HeadlessChrome.FromHtml("<p>Hello, World</p>", options: options);

Convert URL to PDF

var apiResponse = a2pClient.HeadlessChrome.FromUrl("http://www.api2pdf.com");

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

var apiResponse = a2pClient.HeadlessChrome.FromUrl("http://www.api2pdf.com", inline: true, outputFileName: "test.pdf");

Convert URL to PDF (use dictionary for advanced Headless Chrome settings) View full list of Headless Chrome options available.

var options = new Dictionary<string, string>();
options.Add("landscape", "true");
var apiResponse = a2pClient.HeadlessChrome.FromUrl("http://www.api2pdf.com", options: options);

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

var apiResponse = a2pClient.LibreOffice.Convert("https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx");

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

var apiResponse = a2pClient.LibreOffice.Convert("https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx", inline: true, outputFileName: "test.pdf");

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

var links_to_pdfs = new List<string>() {"https://LINK-TO-PDF", "https://LINK-TO-PDF"};
var apiResponse = a2pClient.Merge(links_to_pdfs);

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

var links_to_pdfs = new List<string>() {"https://LINK-TO-PDF", "https://LINK-TO-PDF"};
var apiResponse = a2pClient.Merge(links_to_pdfs, inline: true, outputFileName: "test.pdf");

Helper Methods

Api2PdfResponse Delete(string responseId)

By default, Api2Pdf will delete your PDF 24 hours after it has been generated. For those with high security needs, you may want to delete your PDF on command. You can do so by making an DELETE api call with the responseId attribute that was returned on the original JSON payload.

var a2pClient = Api2Pdf("YOUR-API-KEY");
var apiResponse = a2pClient.HeadlessChrome.FromHtml("<p>Hello World</p>");
var responseId = apiResponse.ResponseId;
//delete PDF
a2pClient.Delete(responseId);

void Api2PdfResponse.SavePdf(string localPath)

On any Api2PdfResponse that succesfully generated a pdf, you can use the handy SavePdf(string localPdf) method to download the pdf to a local cache.

var a2pClient = Api2Pdf("YOUR-API-KEY");
var links_to_pdfs = new List<string>() {"https://LINK-TO-PDF", "https://LINK-TO-PDF"};
var apiResponse = a2pClient.Merge(links_to_pdfs, inline: true, outputFileName: "test.pdf");
apiResponse.SavePdf("path-to-local-folder");

byte[] Api2PdfResponse.GetPdfBytes()

You can use GetPdfBytes() method to download the pdf to a byte array.

var a2pClient = Api2Pdf("YOUR-API-KEY");
var links_to_pdfs = new List<string>() {"https://LINK-TO-PDF", "https://LINK-TO-PDF"};
var apiResponse = a2pClient.Merge(links_to_pdfs, inline: true, outputFileName: "test.pdf");
apiResponse.GetPdfBytes();

api2pdf.dotnet's People

Contributors

apexdodge avatar bootleg224 avatar

Watchers

 avatar  avatar

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.