The Pdfcrowd online API is a professional solution that lets you create PDF from web pages and raw HTML code in your applications. The API is easy to use and the integration takes only a few lines of code.
Here are some samples created with the API. Click a thumbnail to open the PDF.
|
|
|
|
|
| Status report | JavaScript vector chart | Wikipedia page | Invoice | Newsletter |
| PHP Client Library | Java Client Library | DotNET Client Library |
| Python Client Library | Ruby Client Library | Others |
Click your favorite language to see examples of how to create PDF from a web page, an HTML file, and an HTML string:
<?php require 'pdfcrowd.php'; try { // create an API client instance $client = new Pdfcrowd("username", "apikey"); // convert a web page and store the generated PDF into a $pdf variable $pdf = $client->convertURI('http://www.google.com/'); // set HTTP response headers header("Content-Type: application/pdf"); header("Cache-Control: no-cache"); header("Accept-Ranges: none"); header("Content-Disposition: attachment; filename=\"google_com.pdf\""); // send the generated PDF echo $pdf; } catch(PdfcrowdException $e) { echo "Pdfcrowd Error: " . $e->getMessage(); } ?>
convertHtml() method instead of convertURI():
$pdf = $client->convertHtml("<head></head><body>My HTML Layout</body>");
$pdf = $client->convertFile("/path/to/MyLayout.html");
To learn more, see the Pdfcrowd API PHP client documentation.
using System; using System.IO; public class PdfcrowdTest { static void Main() { try { FileStream fileStream; // create an API client instance pdfcrowd.Client client = new pdfcrowd.Client("username", "apikey"); // convert a web page and save the PDF to a file fileStream = new FileStream("google_com.pdf", FileMode.CreateNew); client.convertURI("http://www.google.com", fileStream); fileStream.Close(); // convert an HTML string and store the PDF into a memory stream MemoryStream memStream = new MemoryStream(); string html = "<head></head><body>My HTML Layout</body>"; client.convertHtml(html, memStream); // convert an HTML file fileStream = new FileStream("file.pdf", FileMode.CreateNew); client.convertFile("c:/local/file.html", fileStream); fileStream.Close(); // retrieve the number of tokens in your account int ntokens = client.numTokens(); } catch(pdfcrowd.Error why) { System.Console.WriteLine(why.ToString()); } } }
To learn more, see the Pdfcrowd API .NET client documentation.
<%-- file: PdfGenerator.aspx --%> <%@ Page Language="C#" CodeFile="PdfGenerator.aspx.cs" Inherits="Website.PdfGenerator" AutoEventWireup="true" %>
// file: PdfGenerator.aspx.cs using System; using System.IO; namespace Website { public partial class PdfGenerator : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { System.Web.HttpResponse Response = System.Web.HttpContext.Current.Response; try { // create an API client instance pdfcrowd.Client client = new pdfcrowd.Client("username", "apikey"); // convert a web page and write the generated PDF to a memory stream MemoryStream Stream = new MemoryStream(); client.convertURI("http://www.google.com", Stream); // set HTTP response headers Response.Clear(); Response.AddHeader("Content-Type", "application/pdf"); Response.AddHeader("Cache-Control", "no-cache"); Response.AddHeader("Accept-Ranges", "none"); Response.AddHeader("Content-Disposition", "attachment; filename=google_com.pdf"); // send the generated PDF Stream.WriteTo(Response.OutputStream); Stream.Close(); Response.Flush(); Response.End(); } catch(pdfcrowd.Error why) { Response.Write(why.ToString()); } } } }
convertHtml() method instead of convertURI():
client.convertHtml("<head></head><body>My HTML Layout</body>", Stream);
client.convertFile("c:/MyLayout.html", Stream);
To learn more, see the Pdfcrowd API .NET client documentation.
<%-- file: PdfGenerator.aspx --%> <%@ Page Language="VB" CodeFile="PdfGenerator.aspx.vb" Inherits="Website.PdfGenerator" AutoEventWireup="true" %>
' file: PdfGenerator.aspx.vb Imports System Imports System.IO Namespace Website Public Partial Class PdfGenerator Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Dim Response As System.Web.HttpResponse = System.Web.HttpContext.Current.Response Try ' create an API client instance Dim client As New pdfcrowd.Client("username", "apikey") ' convert a web page and write the generated PDF to a memory stream Dim Stream As New MemoryStream client.convertURI("http://www.google.com", Stream) ' set HTTP response headers Response.Clear() Response.AddHeader("Content-Type", "application/pdf") Response.AddHeader("Cache-Control", "no-cache") Response.AddHeader("Accept-Ranges", "none") Response.AddHeader("Content-Disposition", "attachment; filename=google_com.pdf") ' send the generated PDF Stream.WriteTo(Response.OutputStream) Stream.Close() Response.Flush() Response.End() Catch why As pdfcrowd.Error Response.Write(why.ToString()) End Try End Sub End Class End Namespace
convertHtml() method instead of convertURI():
client.convertHtml("<head></head><body>My HTML Layout</body>", Stream)
client.convertFile("c:/MyLayout.html", Stream)
To learn more, see the Pdfcrowd API .NET client documentation.
import com.pdfcrowd.*; import java.io.*; public class PdfcrowdTest { public static void main(String[] args) { try { FileOutputStream fileStream; // create an API client instance Client client = new Client("username", "apikey"); // convert a web page and save the PDF to a file fileStream = new FileOutputStream("google_com.pdf"); client.convertURI("http://www.google.com/", fileStream); fileStream.close(); // convert an HTML string and store the PDF into a byte array ByteArrayOutputStream memStream = new ByteArrayOutputStream(); String html = "<head></head><body>My HTML Layout</body>"; client.convertHtml(html, memStream); // convert an HTML file fileStream = new FileOutputStream("file.pdf"); client.convertFile("/path/to/local/file.html", fileStream); fileStream.close(); // retrieve the number of tokens in your account Integer ntokens = client.numTokens(); } catch(PdfcrowdError why) { System.err.println(why.getMessage()); } catch(IOException exc) { // handle the exception } } }
To learn more, see the Pdfcrowd API Java client documentation.
import pdfcrowd try: # create an API client instance client = pdfcrowd.Client("username", "apikey") # convert a web page and store the generated PDF into a pdf variable pdf = client.convertURI('http://www.google.com') # convert an HTML string and save the result to a file html="<head></head><body>My HTML Layout</body>" client.convertHtml(html, open('html.pdf', 'wb')) # convert an HTML file client.convertFile('/path/to/MyLayout.html', open('file.pdf', 'wb')) # retrieve the number of conversion tokens in your account ntokens = client.numTokens() except pdfcrowd.Error, why: print 'Failed:', why
To learn more, see the Pdfcrowd API Python client documentation.
import pdfcrowd from django.http import HttpResponse def generate_pdf_view(request): try: # create an API client instance client = pdfcrowd.Client("username", "apikey") # convert a web page and store the generated PDF to a variable pdf = client.convertURI("http://www.google.com") # set HTTP response headers response = HttpResponse(mimetype="application/pdf") response["Cache-Control"] = "no-cache" response["Accept-Ranges"] = "none" response["Content-Disposition"] = "attachment; filename=google_com.pdf" # send the generated PDF response.write(pdf) except pdfcrowd.Error, why: response = HttpResponse(mimetype="text/plain") response.write(why) return response
convertHtml() method instead of convertURI():
pdf = client.convertHtml("<head></head><body>My HTML Layout</body>")
pdf = client.convertFile("/path/to/MyLayout.html")
To learn more, see the Pdfcrowd API Python client documentation.
require 'rubygems' require 'pdfcrowd' begin # create an API client instance client = Pdfcrowd::Client.new("username", "apikey") # convert a web page and store the generated PDF into a pdf variable pdf = client.convertURI('http://www.google.com') # convert an HTML string and save the result to a file html="<head></head><body>My HTML Layout</body>" File.open('html.pdf', 'wb') {|f| client.convertHtml(html, f)} # convert an HTML file File.open('file.pdf', 'wb') {|f| client.convertFile('/path/to/MyLayout.html', f)} # retrieve the number of tokens in your account ntokens = client.numTokens() rescue Pdfcrowd::Error => why print 'FAILED: ', why end
To learn more, see the Pdfcrowd API Ruby client documentation.
require 'rubygems' require 'pdfcrowd' def generatePdf begin # create an API client instance client = Pdfcrowd::Client.new("username", "apikey") # convert a web page and store the generated PDF to a variable pdf = client.convertURI("http://www.google.com") # send the generated PDF send_data(pdf, :filename => "google_com.pdf", :type => "application/pdf", :disposition => "attachment") rescue Pdfcrowd::Error => why render :text => why end end
convertHtml() method instead of convertURI():
pdf = client.convertHtml("<head></head><body>My HTML Layout</body>")
pdf = client.convertFile("/path/to/MyLayout.html")
To learn more, see the Pdfcrowd API Ruby client documentation.
var pdf = require('pdfcrowd'); // create an API client instance var client = new pdf.Pdfcrowd("username", "apikey"); // convert an HTML string and send the generated PDF in a HTTP response client.convertHtml('<html>regular HTML code</html>', pdf.sendHttpResponse(response)); // convert a web page and save it to a file client.convertURI('http://www.google.com', pdf.saveToFile("google_com.pdf")); // convert a local HTML file: client.convertFile('/local/file.html', pdf.saveToFile("file.pdf"));
$ curl -F "username=$username" -F "key=$apikey" -F 'src=http://www.google.com' \ > http://pdfcrowd.com/api/pdf/convert/uri/ > google_com.pdf $ curl -F "username=$username" -F "key=$apikey" -F 'src=@index.html' \ > http://pdfcrowd.com/api/pdf/convert/html/ > file.pdf $ html_producer | curl -F "username=$username" -F "key=$apikey" -F 'src=<-' \ > http://pdfcrowd.com/api/pdf/convert/html/ > html.pdf
To learn more, see the REST API documentation.
$ pdfcrowd.sh http://www.google.com > google_com.pdf $ pdfcrowd.sh /path/to/html_file > html.pdf $ html_producer | pdfcrowd.sh - > file.pdf
To learn more, see the pdfcrowd.sh documentation.
Testing the integration with your application is free. We will credit your account with enough free conversions so you can make sure that everything works to your satisfaction before going to production.
If you have any questions, please do not hesitate to ask. We will gladly help you on our support forums.
An API call is made by sending an HTTP request to the API endpoint with parameters passed as POST data. The API endpoints support both http and https schemes.
The following parameters are used for authentication and must be present in every API call.
| Parameter | Description |
|---|---|
| username | Your username at Pdfcrowd |
| key | API key. Can be found on your account page. Note that the key is regenerated when you change your password. |
A PDF document can be created in one of the following ways. The created PDF is returned in the response body.
| Convert a web page | |
|---|---|
| Method | POST |
| API Endpoint | http://pdfcrowd.com/api/pdf/convert/uri/ |
| Content Type | application/x-www-form-urlencoded |
| Post Data |
Mandatory
|
| Convert a local HTML file | |
|---|---|
| Method | POST |
| API Endpoint | http://pdfcrowd.com/api/pdf/convert/html/ |
| Content Type | multipart/form-data |
| Post Data |
Mandatory
|
| Convert raw HTML code | |
|---|---|
| Method | POST |
| Content Type | application/x-www-form-urlencoded |
| API Endpoint | http://pdfcrowd.com/api/pdf/convert/html/ |
| Post Data |
Mandatory
|
The following optional parameters control properties of the generated PDF file.
Page Setup
| Parameter | Description |
|---|---|
| width | PDF page width in units. |
| height | PDF page height in units. -1 for a single page PDF. |
| hmargin | PDF page horizontal margin in units. |
| vmargin | PDF page vertical margin in units. |
Page dimensions and margins can be specified in inches (in), millimeters (mm), centimeters (cm), or points (pt). If no units are specified, points are assumed.
Examples: 210mm, 8.5in
Headers and Footers
| Parameter | Description |
|---|---|
| footer_text |
Place plain text inside the page footer. The following variables are expanded:
|
| footer_html | Place the specified HTML code inside the page footer. See footer_text for the list of variables that are expanded. |
| footer_url | Load HTML code from the specified URL and place it inside the page footer. See footer_text for the list of variables that are expanded. |
| header_html | Place the specified HTML code inside the page header. See footer_text for the list of variables that are expanded. |
| header_url | Load HTML code from the specified URL and place it inside the page header. See footer_text for the list of variables that are expanded. |
HTML options
PDF Options
| Parameter | Description |
|---|---|
| encrypted | Encrypt the PDF. This prevents search engines from indexing the document. |
| author | Set the PDF author field. |
| user_pwd | Protect the PDF with an user password. When a PDF has an user password, it must be supplied in order to view the document and to perform operations allowed by the access permissions. At most 32 characters. |
| owner_pwd | Protect the PDF with an owner password. Supplying an owner password grants unlimited access to the PDF including changing the passwords and access permissions. At most 32 characters. |
| no_print | Do not allow to print the PDF. |
| no_modify | Do not allow to modify the PDF. |
| no_copy | Do not allow to extract text and graphics from the PDF. |
| page_layout |
Specifies the initial page layout when the PDF is opened in a viewer:
|
| initial_pdf_zoom_type |
Specifies the initial page zoom type when the PDF is opened in a viewer:
|
| initial_pdf_zoom | Specifies the initial page zoom when the PDF is opened in a viewer. Defaults to 100%. |
| page_mode | Specifies the appearance of the PDF when opened:
|
| max_pages | Print at most the specified number of pages. |
| pdf_name | The file name of the created PDF (max 180 chars). If not specified then the name is auto-generated. |
| pdf_scaling_factor | The scaling factor used to convert between HTML and PDF. The default value is 1.333 (4/3) which makes the PDF content up to 1/3 larger than HTML. |
| page_background_color | The page background color in RRGGBB hexadecimal format. |
| transparent_background | Do not print the body background. Requires the following CSS rule to be declared:body { background-color:rgba(255,255,255,0.0); }
|
Miscellaneous
The following command converts www.google.com to PDF:
$ curl -F "username=$username" -F "key=$apikey" \ > -F 'src=http://www.google.com' \ > http://pdfcrowd.com/api/pdf/convert/uri/ > google_com.pdf
The following command converts the output of some html_producer
application to PDF and protects it with an user password:
$ html_producer | curl -F "username=$username" -F "key=$apikey" \ > -F 'src=<-' \ > -F 'user_pwd=secret' \ > http://pdfcrowd.com/api/pdf/convert/html/ > google_com.pdf
The following command converts a local file index.html to PDF
and disables printing:
$ curl -F "username=$username" -F "key=$apikey" \ > -F 'src=@index.html' \ > -F 'no_print=1' \ > http://pdfcrowd.com/api/pdf/convert/html/ > google_com.pdf
Note, that the examples above ignore errors. If the API call
fails google_com.pdf will contain an error message instead of
PDF. For convenience, Pdfcrowd provides a shell wrapper
around curl that checks for errors and generally makes the
interaction with the API from the command line friendlier.
| Number of remaining tokens | |
|---|---|
| Method | POST |
| API Endpoint | http://pdfcrowd.com/api/user/<username>/tokens/ |
| Content Type | application/x-www-form-urlencoded |
| Post Data | Mandatory |
The response contains the result in plain text.
$ curl -F "username=$username" -F "key=$apikey" "http://pdfcrowd.com/api/user/$username/tokens/" 12399
The API returns the standard HTTP response status codes. These are the most important ones:
| Code | Description |
|---|---|
| 200 OK | The API call succeeded. |
| 400 Bad Request | The user sent an invalid request. The body of the response contains an explanation in plain text. |
| 413 Request Entity Too Large | See Limitations below. |
| 502 Bad Gateway | See Limitations below. |
| 503 Service Unavailable | See Limitations below. |
| 510 | A non-standard status code indicating that the server couldn't process the request. The body of the response contains an explanation in plain text. |
Pdfcrowd provides a
shell
script which wraps curl and provides more convenient access
to the API from shell.
The script reads options from ~/.pdfcrowd and then from the
command line. You can store your default options to ~/.pdfcrowd,
one option per line:
$ cat > ~/.pdfcrowd <<! > -username "$username" > -key "$apikey" > -width 210mm > -height 297mm > !
Now, you can run:
$ pdfcrowd.sh http://www.google.com > google_com.pdf $ pdfcrowd.sh /path/to/local/file > html.pdf $ html_producer | pdfcrowd.sh - > file.pdf $ echo "remaining tokens: $(pdfcrowd.sh @)"
On success, exit status 0 is returned and the result is written
to stdout. Otherwise, non-zero exit status is returned and the
error message is written to stderr. Run the script
with -help to get the list of available options.
We understand that your data may be sensitive and confidential and that it is absolutely unacceptable to disclose it or keep unnecessary copies.
We want to be transparent about how we process your data, so here we describe an API conversion request lifecycle:
The following limits are applied to ensure fair distribution of capacity.
If you find the limits too restrictive we can provision a private server optimized for your specific needs with the limits lifted. If you would like to discuss this option please contact us.
The API returns an HTTP 503 Service unavailable status code in the following cases:
Ideally, you should serialize your API calls. However, we understand that you can't always ensure this in some environments, such as for instance with Google App Engine Task Queue. In such cases it is perfectly acceptable to check for this error and possibly send the failed request again.
If a request takes more than 40 seconds to complete it is cancelled and either of these messages may be returned:
A typical cause of this error is too many images on an HTML page which take too long to download. Another cause might be a long running JavaScript.
If the size of the uploaded data exceeds 4MB then an HTTP 413 Request entity too large error is returned. You can zip your HTML to avoid this error.
If the size of the downloaded data exceeds 100MB then the an HTTP 502 error code is returned.
We return an HTTP 502 error code also when: