HTML to PDF API - Ruby SDK

Convert web pages and HTML documents to PDF in Ruby using the Pdfcrowd API v2. The API is easy to use and the integration takes only a couple of lines of code.

Installation

Install the client library from rubygems.org
 $ gem install pdfcrowd

You can learn more about other install options here.

Authentication

Authentication is needed in order to use the Pdfcrowd API. The credentials used for accessing the API are your Pdfcrowd username and the API key. You can sign up for the Pdfcrowd API here.

Examples

Convert a web page to a PDF file
require 'pdfcrowd'

begin
    # create the API client instance
    client = Pdfcrowd::HtmlToPdfClient.new("your_username", "your_apikey")

    # run the conversion and write the result to a file
    client.convertUrlToFile("http://www.example.com", 'example.pdf')
rescue Pdfcrowd::Error => why
    # report the error
    STDERR.puts "Pdfcrowd Error: #{why}"

    # handle the exception here or rethrow and handle it at a higher level
    raise
end
Convert a web page to in-memory PDF
require 'pdfcrowd'

begin
    # create the API client instance
    client = Pdfcrowd::HtmlToPdfClient.new("your_username", "your_apikey")

    # create output file for conversion result
    output_file = open("example.pdf", "wb")

    # run the conversion and store the result into a pdf variable
    pdf = client.convertUrl("http://www.example.com")

    # write the pdf the into the output file
    output_file.write(pdf)

    # close the output file
    output_file.close()
rescue Pdfcrowd::Error => why
    # report the error
    STDERR.puts "Pdfcrowd Error: #{why}"

    # handle the exception here or rethrow and handle it at a higher level
    raise
end
Convert a web page and write the resulting PDF to an output stream
require 'pdfcrowd'

begin
    # create the API client instance
    client = Pdfcrowd::HtmlToPdfClient.new("your_username", "your_apikey")

    # create output stream for conversion result
    output_stream = open("example.pdf", "wb")

    # run the conversion and write the result into the output stream
    client.convertUrlToStream("http://www.example.com", output_stream)

    # close the output stream
    output_stream.close()
rescue Pdfcrowd::Error => why
    # report the error
    STDERR.puts "Pdfcrowd Error: #{why}"

    # handle the exception here or rethrow and handle it at a higher level
    raise
end
Convert a local HTML file to a PDF file
require 'pdfcrowd'

begin
    # create the API client instance
    client = Pdfcrowd::HtmlToPdfClient.new("your_username", "your_apikey")

    # run the conversion and write the result to a file
    client.convertFileToFile("/path/to/MyLayout.html", 'MyLayout.pdf')
rescue Pdfcrowd::Error => why
    # report the error
    STDERR.puts "Pdfcrowd Error: #{why}"

    # handle the exception here or rethrow and handle it at a higher level
    raise
end
Convert a local HTML file to in-memory PDF
require 'pdfcrowd'

begin
    # create the API client instance
    client = Pdfcrowd::HtmlToPdfClient.new("your_username", "your_apikey")

    # create output file for conversion result
    output_file = open("MyLayout.pdf", "wb")

    # run the conversion and store the result into a pdf variable
    pdf = client.convertFile("/path/to/MyLayout.html")

    # write the pdf the into the output file
    output_file.write(pdf)

    # close the output file
    output_file.close()
rescue Pdfcrowd::Error => why
    # report the error
    STDERR.puts "Pdfcrowd Error: #{why}"

    # handle the exception here or rethrow and handle it at a higher level
    raise
end
Convert a local HTML file and write the resulting PDF to an output stream
require 'pdfcrowd'

begin
    # create the API client instance
    client = Pdfcrowd::HtmlToPdfClient.new("your_username", "your_apikey")

    # create output stream for conversion result
    output_stream = open("MyLayout.pdf", "wb")

    # run the conversion and write the result into the output stream
    client.convertFileToStream("/path/to/MyLayout.html", output_stream)

    # close the output stream
    output_stream.close()
rescue Pdfcrowd::Error => why
    # report the error
    STDERR.puts "Pdfcrowd Error: #{why}"

    # handle the exception here or rethrow and handle it at a higher level
    raise
end
Convert a string containing HTML to a PDF file
require 'pdfcrowd'

begin
    # create the API client instance
    client = Pdfcrowd::HtmlToPdfClient.new("your_username", "your_apikey")

    # run the conversion and write the result to a file
    client.convertStringToFile("<html><body><h1>Hello World!</h1></body></html>", 'HelloWorld.pdf')
rescue Pdfcrowd::Error => why
    # report the error
    STDERR.puts "Pdfcrowd Error: #{why}"

    # handle the exception here or rethrow and handle it at a higher level
    raise
end
Convert a string containing HTML to in-memory PDF
require 'pdfcrowd'

begin
    # create the API client instance
    client = Pdfcrowd::HtmlToPdfClient.new("your_username", "your_apikey")

    # create output file for conversion result
    output_file = open("HelloWorld.pdf", "wb")

    # run the conversion and store the result into a pdf variable
    pdf = client.convertString("<html><body><h1>Hello World!</h1></body></html>")

    # write the pdf the into the output file
    output_file.write(pdf)

    # close the output file
    output_file.close()
rescue Pdfcrowd::Error => why
    # report the error
    STDERR.puts "Pdfcrowd Error: #{why}"

    # handle the exception here or rethrow and handle it at a higher level
    raise
end
Convert a string containing HTML and write the resulting PDF to an output stream
require 'pdfcrowd'

begin
    # create the API client instance
    client = Pdfcrowd::HtmlToPdfClient.new("your_username", "your_apikey")

    # create output stream for conversion result
    output_stream = open("HelloWorld.pdf", "wb")

    # run the conversion and write the result into the output stream
    client.convertStringToStream("<html><body><h1>Hello World!</h1></body></html>", output_stream)

    # close the output stream
    output_stream.close()
rescue Pdfcrowd::Error => why
    # report the error
    STDERR.puts "Pdfcrowd Error: #{why}"

    # handle the exception here or rethrow and handle it at a higher level
    raise
end
Get info about the current conversion
require 'pdfcrowd'

begin
    # create the API client instance
    client = Pdfcrowd::HtmlToPdfClient.new("your_username", "your_apikey")

    # configure the conversion
    client.setDebugLog(true)

    # run the conversion and write the result to a file
    client.convertFileToFile("/path/to/MyLayout.html", 'MyLayout.pdf')
    
    # print URL to the debug log
    puts "Debug log url: #{client.getDebugLogUrl()}"
    
    # print the number of available conversion credits in your account
    puts "Remaining credit count: #{client.getRemainingCreditCount()}"
    
    # print the number of credits consumed by the conversion
    puts "Consumed credit count: #{client.getConsumedCreditCount()}"
    
    # print the unique ID of the conversion
    puts "Job id: #{client.getJobId()}"
    
    # print the total number of pages in the output document
    puts "Page count: #{client.getPageCount()}"
    
    # print the size of the output in bytes
    puts "Output size: #{client.getOutputSize()}"
rescue Pdfcrowd::Error => why
    # report the error
    STDERR.puts "Pdfcrowd Error: #{why}"

    # handle the exception here or rethrow and handle it at a higher level
    raise
end

Advanced Examples

Customize the page size and the orientation
require 'pdfcrowd'

begin
    # create the API client instance
    client = Pdfcrowd::HtmlToPdfClient.new("your_username", "your_apikey")

    # configure the conversion
    client.setPageSize("Letter")
    client.setOrientation("landscape")
    client.setNoMargins(true)

    # run the conversion and write the result to a file
    client.convertUrlToFile("http://www.example.com", 'letter_landscape.pdf')
rescue Pdfcrowd::Error => why
    # report the error
    STDERR.puts "Pdfcrowd Error: #{why}"

    # handle the exception here or rethrow and handle it at a higher level
    raise
end
Put the source URL in the header and the page number in the footer
require 'pdfcrowd'

begin
    # create the API client instance
    client = Pdfcrowd::HtmlToPdfClient.new("your_username", "your_apikey")

    # configure the conversion
    client.setHeaderHeight("15mm")
    client.setFooterHeight("10mm")
    client.setHeaderHtml("<a class='pdfcrowd-source-url' data-pdfcrowd-placement='href-and-content'></a>")
    client.setFooterHtml("<center><span class='pdfcrowd-page-number'></span></center>")
    client.setMarginTop("0mm")
    client.setMarginBottom("0mm")

    # run the conversion and write the result to a file
    client.convertUrlToFile("http://www.example.com", 'header_footer.pdf')
rescue Pdfcrowd::Error => why
    # report the error
    STDERR.puts "Pdfcrowd Error: #{why}"

    # handle the exception here or rethrow and handle it at a higher level
    raise
end
Zoom the HTML document
require 'pdfcrowd'

begin
    # create the API client instance
    client = Pdfcrowd::HtmlToPdfClient.new("your_username", "your_apikey")

    # configure the conversion
    client.setScaleFactor(300)

    # run the conversion and write the result to a file
    client.convertUrlToFile("http://www.example.com", 'zoom_300.pdf')
rescue Pdfcrowd::Error => why
    # report the error
    STDERR.puts "Pdfcrowd Error: #{why}"

    # handle the exception here or rethrow and handle it at a higher level
    raise
end
Set PDF metadata
require 'pdfcrowd'

begin
    # create the API client instance
    client = Pdfcrowd::HtmlToPdfClient.new("your_username", "your_apikey")

    # configure the conversion
    client.setAuthor("Pdfcrowd")
    client.setTitle("Hello")
    client.setSubject("Demo")
    client.setKeywords("Pdfcrowd,demo")

    # run the conversion and write the result to a file
    client.convertUrlToFile("http://www.example.com", 'with_metadata.pdf')
rescue Pdfcrowd::Error => why
    # report the error
    STDERR.puts "Pdfcrowd Error: #{why}"

    # handle the exception here or rethrow and handle it at a higher level
    raise
end
Create a Powerpoint like presentation from an HTML document
require 'pdfcrowd'

begin
    # create the API client instance
    client = Pdfcrowd::HtmlToPdfClient.new("your_username", "your_apikey")

    # configure the conversion
    client.setPageLayout("single-page")
    client.setPageMode("full-screen")
    client.setInitialZoomType("fit-page")
    client.setOrientation("landscape")
    client.setNoMargins(true)

    # run the conversion and write the result to a file
    client.convertUrlToFile("https://pdfcrowd.com/doc/api/", 'slide_show.pdf')
rescue Pdfcrowd::Error => why
    # report the error
    STDERR.puts "Pdfcrowd Error: #{why}"

    # handle the exception here or rethrow and handle it at a higher level
    raise
end
Convert an HTML document section
require 'pdfcrowd'

begin
    # create the API client instance
    client = Pdfcrowd::HtmlToPdfClient.new("your_username", "your_apikey")

    # configure the conversion
    client.setElementToConvert("#main")

    # run the conversion and write the result to a file
    client.convertUrlToFile("https://pdfcrowd.com/doc/api/", 'html_part.pdf')
rescue Pdfcrowd::Error => why
    # report the error
    STDERR.puts "Pdfcrowd Error: #{why}"

    # handle the exception here or rethrow and handle it at a higher level
    raise
end
Inject an HTML code
require 'pdfcrowd'

begin
    # create the API client instance
    client = Pdfcrowd::HtmlToPdfClient.new("your_username", "your_apikey")

    # configure the conversion
    client.setCustomJavascript("el=document.createElement('h2'); el.textContent='Hello from Pdfcrowd API'; el.style.color='red'; el_before=document.getElementsByTagName('h1')[0]; el_before.parentNode.insertBefore(el, el_before.nextSibling)")

    # run the conversion and write the result to a file
    client.convertUrlToFile("http://www.example.com", 'html_inject.pdf')
rescue Pdfcrowd::Error => why
    # report the error
    STDERR.puts "Pdfcrowd Error: #{why}"

    # handle the exception here or rethrow and handle it at a higher level
    raise
end
Renderer debugging - highlight HTML elements
require 'pdfcrowd'

begin
    # create the API client instance
    client = Pdfcrowd::HtmlToPdfClient.new("your_username", "your_apikey")

    # configure the conversion
    client.setCustomJavascript("libPdfcrowd.highlightHtml(false, true, true, false)")

    # run the conversion and write the result to a file
    client.convertUrlToFile("http://www.example.com", 'highlight_background.pdf')
rescue Pdfcrowd::Error => why
    # report the error
    STDERR.puts "Pdfcrowd Error: #{why}"

    # handle the exception here or rethrow and handle it at a higher level
    raise
end
Renderer debugging - borders with spacing around HTML elements
require 'pdfcrowd'

begin
    # create the API client instance
    client = Pdfcrowd::HtmlToPdfClient.new("your_username", "your_apikey")

    # configure the conversion
    client.setCustomJavascript("libPdfcrowd.highlightHtml(true, false, true, true)")

    # run the conversion and write the result to a file
    client.convertUrlToFile("http://www.example.com", 'highlight_borders.pdf')
rescue Pdfcrowd::Error => why
    # report the error
    STDERR.puts "Pdfcrowd Error: #{why}"

    # handle the exception here or rethrow and handle it at a higher level
    raise
end

Tips & Tricks

The API lets you convert a web page, a local HTML file, or a string containing HTML. The result of the conversion can be stored to a local file, to a stream object or to a variable. See the conversion input section for more details.

The best way to start with the API is to choose one of the examples and once you get it working, you can:

You can also use these HTML related features:

  • You can use the following classes in your HTML code which hide/remove elements from the output:
    • pdfcrowd-remove - sets display:none on the element
    • pdfcrowd-hide - sets visibility:hidden on the element
  • You can switch to the print version of the page (if it exists) with setUsePrintMedia.
  • You can force a page break with
    <div style="page-break-before:always"></div>
  • You can avoid a page break inside an element with the following CSS
    img { page-break-inside:avoid }
  • You can use setCustomJavascript to alter the HTML contents with a custom JavaScript.

API Reference - class HtmlToPdfClient

Conversion from HTML to PDF.

Constructor

def initialize(user_name, api_key)
Constructor for the Pdfcrowd API client.
Parameter Description Default
user_name
Your username at Pdfcrowd.
api_key
Your API key.

 

Conversion Input

def convertUrl(url)
Convert a web page.
Parameter Description Default
url
The address of the web page to convert.
The supported protocols are http:// and https://.
Returns
  • byte[] - Byte array containing the conversion output.
def convertUrlToStream(url, out_stream)
Convert a web page and write the result to an output stream.
Parameter Description Default
url
The address of the web page to convert.
The supported protocols are http:// and https://.
out_stream
The output stream that will contain the conversion output.
def convertUrlToFile(url, file_path)
Convert a web page and write the result to a local file.
Parameter Description Default
url
The address of the web page to convert.
The supported protocols are http:// and https://.
file_path
The output file path.
The string must not be empty.
def convertFile(file)
Convert a local file.
Parameter Description Default
file
The path to a local file to convert.
The file can be either a single file or an archive (.tar.gz, .tar.bz2, or .zip).
If the HTML document refers to local external assets (images, style sheets, javascript), zip the document together with the assets.
The file must exist and not be empty.
The file name must have a valid extension.
Returns
  • byte[] - Byte array containing the conversion output.
def convertFileToStream(file, out_stream)
Convert a local file and write the result to an output stream.
Parameter Description Default
file
The path to a local file to convert.
The file can be either a single file or an archive (.tar.gz, .tar.bz2, or .zip).
If the HTML document refers to local external assets (images, style sheets, javascript), zip the document together with the assets.
The file must exist and not be empty.
The file name must have a valid extension.
out_stream
The output stream that will contain the conversion output.
def convertFileToFile(file, file_path)
Convert a local file and write the result to a local file.
Parameter Description Default
file
The path to a local file to convert.
The file can be either a single file or an archive (.tar.gz, .tar.bz2, or .zip).
If the HTML document refers to local external assets (images, style sheets, javascript), zip the document together with the assets.
The file must exist and not be empty.
The file name must have a valid extension.
file_path
The output file path.
The string must not be empty.
def convertString(text)
Convert a string.
Parameter Description Default
text
The string content to convert.
The string must not be empty.
Returns
  • byte[] - Byte array containing the conversion output.
def convertStringToStream(text, out_stream)
Convert a string and write the output to an output stream.
Parameter Description Default
text
The string content to convert.
The string must not be empty.
out_stream
The output stream that will contain the conversion output.
def convertStringToFile(text, file_path)
Convert a string and write the output to a file.
Parameter Description Default
text
The string content to convert.
The string must not be empty.
file_path
The output file path.
The string must not be empty.

 

Page setup

def setPageSize(page_size)
Set the output page size.
Parameter Description Default
page_size
Allowed values:
  • A2
  • A3
  • A4
  • A5
  • A6
  • Letter
A4
Returns
  • HtmlToPdfClient - The converter object.
def setPageWidth(page_width)
Set the output page width. The safe maximum is 200in otherwise some PDF viewers may be unable to open the PDF.
Parameter Description Default
page_width
Can be specified in inches (in), millimeters (mm), centimeters (cm), or points (pt).
8.27in
Returns
  • HtmlToPdfClient - The converter object.
Examples:
  • setPageWidth("300mm")
  • setPageWidth("9.5in")
def setPageHeight(page_height)
Set the output page height. Use -1 for a single page PDF. The safe maximum is 200in otherwise some PDF viewers may be unable to open the PDF.
Parameter Description Default
page_height
Can be -1 or specified in inches (in), millimeters (mm), centimeters (cm), or points (pt).
11.7in
Returns
  • HtmlToPdfClient - The converter object.
Examples:
  • setPageHeight("350mm")
  • setPageHeight("15.25in")
  • The height of the page is calculated automatically so that the whole document fits into it.
    setPageHeight("-1")
def setPageDimensions(width, height)
Set the output page dimensions.
Parameter Description Default
width
Set the output page width. The safe maximum is 200in otherwise some PDF viewers may be unable to open the PDF.
Can be specified in inches (in), millimeters (mm), centimeters (cm), or points (pt).
8.27in
height
Set the output page height. Use -1 for a single page PDF. The safe maximum is 200in otherwise some PDF viewers may be unable to open the PDF.
Can be -1 or specified in inches (in), millimeters (mm), centimeters (cm), or points (pt).
11.7in
Returns
  • HtmlToPdfClient - The converter object.
Examples:
  • setPageDimensions("300mm", "350mm")
  • setPageDimensions("9.5in", "15.25in")
  • setPageDimensions("372mm", "520pt")
def setOrientation(orientation)
Set the output page orientation.
Parameter Description Default
orientation
Allowed values:
  • landscape
  • portrait
portrait
Returns
  • HtmlToPdfClient - The converter object.
def setMarginTop(margin_top)
Set the output page top margin.
Parameter Description Default
margin_top
Can be specified in inches (in), millimeters (mm), centimeters (cm), or points (pt).
0.4in
Returns
  • HtmlToPdfClient - The converter object.
Examples:
  • setMarginTop("1in")
  • setMarginTop("2.5cm")
def setMarginRight(margin_right)
Set the output page right margin.
Parameter Description Default
margin_right
Can be specified in inches (in), millimeters (mm), centimeters (cm), or points (pt).
0.4in
Returns
  • HtmlToPdfClient - The converter object.
Examples:
  • setMarginRight("1in")
  • setMarginRight("2.5cm")
def setMarginBottom(margin_bottom)
Set the output page bottom margin.
Parameter Description Default
margin_bottom
Can be specified in inches (in), millimeters (mm), centimeters (cm), or points (pt).
0.4in
Returns
  • HtmlToPdfClient - The converter object.
Examples:
  • setMarginBottom("1in")
  • setMarginBottom("2.5cm")
def setMarginLeft(margin_left)
Set the output page left margin.
Parameter Description Default
margin_left
Can be specified in inches (in), millimeters (mm), centimeters (cm), or points (pt).
0.4in
Returns
  • HtmlToPdfClient - The converter object.
Examples:
  • setMarginLeft("1in")
  • setMarginLeft("2.5cm")
def setNoMargins(no_margins)
Disable margins.
Parameter Description Default
no_margins
Set to true to disable margins.
false
Returns
  • HtmlToPdfClient - The converter object.
def setPageMargins(top, right, bottom, left)
Set the output page margins.
Parameter Description Default
top
Set the output page top margin.
Can be specified in inches (in), millimeters (mm), centimeters (cm), or points (pt).
0.4in
right
Set the output page right margin.
Can be specified in inches (in), millimeters (mm), centimeters (cm), or points (pt).
0.4in
bottom
Set the output page bottom margin.
Can be specified in inches (in), millimeters (mm), centimeters (cm), or points (pt).
0.4in
left
Set the output page left margin.
Can be specified in inches (in), millimeters (mm), centimeters (cm), or points (pt).
0.4in
Returns
  • HtmlToPdfClient - The converter object.
def setHeaderUrl(header_url)
Load an HTML code from the specified URL and use it as the page header. The following classes can be used in the HTML. The content of the respective elements will be expanded as follows:
  • pdfcrowd-page-count - the total page count of printed pages
  • pdfcrowd-page-number - the current page number
  • pdfcrowd-source-url - the source URL of a converted document
The following attributes can be used:
  • data-pdfcrowd-number-format - specifies the type of the used numerals
    • Arabic numerals are used by default.
    • Roman numerals can be generated by the roman and roman-lowercase values
    • Example: <span class='pdfcrowd-page-number' data-pdfcrowd-number-format='roman'></span>
  • data-pdfcrowd-placement - specifies where to place the source URL, allowed values:
    • The URL is inserted to the content
      • Example: <span class='pdfcrowd-source-url'></span>
        will produce <span>http://example.com</span>
    • href - the URL is set to the href attribute
      • Example: <a class='pdfcrowd-source-url' data-pdfcrowd-placement='href'>Link to source</a>
        will produce <a href='http://example.com'>Link to source</a>
    • href-and-content - the URL is set to the href attribute and to the content
      • Example: <a class='pdfcrowd-source-url' data-pdfcrowd-placement='href-and-content'></a>
        will produce <a href='http://example.com'>http://example.com</a>
Parameter Description Default
header_url
The supported protocols are http:// and https://.
Returns
  • HtmlToPdfClient - The converter object.
Examples:
  • setHeaderUrl("http://myserver.com/header.html")
def setHeaderHtml(header_html)
Use the specified HTML code as the page header. The following classes can be used in the HTML. The content of the respective elements will be expanded as follows:
  • pdfcrowd-page-count - the total page count of printed pages
  • pdfcrowd-page-number - the current page number
  • pdfcrowd-source-url - the source URL of a converted document
The following attributes can be used:
  • data-pdfcrowd-number-format - specifies the type of the used numerals
    • Arabic numerals are used by default.
    • Roman numerals can be generated by the roman and roman-lowercase values
    • Example: <span class='pdfcrowd-page-number' data-pdfcrowd-number-format='roman'></span>
  • data-pdfcrowd-placement - specifies where to place the source URL, allowed values:
    • The URL is inserted to the content
      • Example: <span class='pdfcrowd-source-url'></span>
        will produce <span>http://example.com</span>
    • href - the URL is set to the href attribute
      • Example: <a class='pdfcrowd-source-url' data-pdfcrowd-placement='href'>Link to source</a>
        will produce <a href='http://example.com'>Link to source</a>
    • href-and-content - the URL is set to the href attribute and to the content
      • Example: <a class='pdfcrowd-source-url' data-pdfcrowd-placement='href-and-content'></a>
        will produce <a href='http://example.com'>http://example.com</a>
Parameter Description Default
header_html
The string must not be empty.
Returns
  • HtmlToPdfClient - The converter object.
Examples:
  • It displays the page number and the total page count.
    setHeaderHtml("Page <span class='pdfcrowd-page-number'></span> of <span class='pdfcrowd-page-count'></span> pages")
def setHeaderHeight(header_height)
Set the header height.
Parameter Description Default
header_height
Can be specified in inches (in), millimeters (mm), centimeters (cm), or points (pt).
0.5in
Returns
  • HtmlToPdfClient - The converter object.
Examples:
  • setHeaderHeight("30mm")
  • setHeaderHeight("1in")
Load an HTML code from the specified URL and use it as the page footer. The following classes can be used in the HTML. The content of the respective elements will be expanded as follows:
  • pdfcrowd-page-count - the total page count of printed pages
  • pdfcrowd-page-number - the current page number
  • pdfcrowd-source-url - the source URL of a converted document
The following attributes can be used:
  • data-pdfcrowd-number-format - specifies the type of the used numerals
    • Arabic numerals are used by default.
    • Roman numerals can be generated by the roman and roman-lowercase values
    • Example: <span class='pdfcrowd-page-number' data-pdfcrowd-number-format='roman'></span>
  • data-pdfcrowd-placement - specifies where to place the source URL, allowed values:
    • The URL is inserted to the content
      • Example: <span class='pdfcrowd-source-url'></span>
        will produce <span>http://example.com</span>
    • href - the URL is set to the href attribute
      • Example: <a class='pdfcrowd-source-url' data-pdfcrowd-placement='href'>Link to source</a>
        will produce <a href='http://example.com'>Link to source</a>
    • href-and-content - the URL is set to the href attribute and to the content
      • Example: <a class='pdfcrowd-source-url' data-pdfcrowd-placement='href-and-content'></a>
        will produce <a href='http://example.com'>http://example.com</a>
Parameter Description Default
Returns
  • HtmlToPdfClient - The converter object.
Examples:
  • setFooterUrl("http://myserver.com/header.html")
Use the specified HTML as the page footer. The following classes can be used in the HTML. The content of the respective elements will be expanded as follows:
  • pdfcrowd-page-count - the total page count of printed pages
  • pdfcrowd-page-number - the current page number
  • pdfcrowd-source-url - the source URL of a converted document
The following attributes can be used:
  • data-pdfcrowd-number-format - specifies the type of the used numerals
    • Arabic numerals are used by default.
    • Roman numerals can be generated by the roman and roman-lowercase values
    • Example: <span class='pdfcrowd-page-number' data-pdfcrowd-number-format='roman'></span>
  • data-pdfcrowd-placement - specifies where to place the source URL, allowed values:
    • The URL is inserted to the content
      • Example: <span class='pdfcrowd-source-url'></span>
        will produce <span>http://example.com</span>
    • href - the URL is set to the href attribute
      • Example: <a class='pdfcrowd-source-url' data-pdfcrowd-placement='href'>Link to source</a>
        will produce <a href='http://example.com'>Link to source</a>
    • href-and-content - the URL is set to the href attribute and to the content
      • Example: <a class='pdfcrowd-source-url' data-pdfcrowd-placement='href-and-content'></a>
        will produce <a href='http://example.com'>http://example.com</a>
Parameter Description Default
Returns
  • HtmlToPdfClient - The converter object.
Examples:
  • It displays the page number and the total page count.
    setFooterHtml("Page <span class='pdfcrowd-page-number'></span> of <span class='pdfcrowd-page-count'></span> pages")
Set the footer height.
Parameter Description Default
Returns
  • HtmlToPdfClient - The converter object.
Examples:
  • setFooterHeight("30mm")
  • setFooterHeight("1in")
def setPrintPageRange(pages)
Set the page range to print.
Parameter Description Default
pages
A comma seperated list of page numbers or ranges.
Returns
  • HtmlToPdfClient - The converter object.
Examples:
  • Just the second page is printed.
    setPrintPageRange("2")
  • The first and the third page are printed.
    setPrintPageRange("1,3")
  • Everything except the first page is printed.
    setPrintPageRange("2-")
  • Just first 3 pages are printed.
    setPrintPageRange("-3")
  • Pages 3, 6, 7, 8 and 9 are printed.
    setPrintPageRange("3,6-9")
def setPageWatermark(page_watermark)
Apply the first page of the watermark PDF to every page of the output PDF.
Parameter Description Default
page_watermark
The file path to a local watermark PDF file.
The file must exist and not be empty.
Returns
  • HtmlToPdfClient - The converter object.
def setMultipageWatermark(multipage_watermark)
Apply each page of the specified watermark PDF to the corresponding page of the output PDF.
Parameter Description Default
multipage_watermark
The file path to a local watermark PDF file.
The file must exist and not be empty.
Returns
  • HtmlToPdfClient - The converter object.
def setPageBackground(page_background)
Apply the first page of the specified PDF to the background of every page of the output PDF.
Parameter Description Default
page_background
The file path to a local background PDF file.
The file must exist and not be empty.
Returns
  • HtmlToPdfClient - The converter object.
def setMultipageBackground(multipage_background)
Apply each page of the specified PDF to the background of the corresponding page of the output PDF.
Parameter Description Default
multipage_background
The file path to a local background PDF file.
The file must exist and not be empty.
Returns
  • HtmlToPdfClient - The converter object.
def setExcludeHeaderOnPages(pages)
The page header is not printed on the specified pages.
Parameter Description Default
pages
List of physical page numbers. Negative numbers count backwards from the last page: -1 is the last page, -2 is the last but one page, and so on.
A comma seperated list of page numbers.
Returns
  • HtmlToPdfClient - The converter object.
Examples:
  • The header is not printed on the second page.
    setExcludeHeaderOnPages("2")
  • The header is not printed on the first and the last page.
    setExcludeHeaderOnPages("1,-1")
The page footer is not printed on the specified pages.
Parameter Description Default
Returns
  • HtmlToPdfClient - The converter object.
Examples:
  • The footer is not printed on the second page.
    setExcludeFooterOnPages("2")
  • The footer is not printed on the first and the last page.
    setExcludeFooterOnPages("1,-1")
def setPageNumberingOffset(offset)
Set an offset between physical and logical page numbers.
Parameter Description Default
offset
Integer specifying page offset.
0
Returns
  • HtmlToPdfClient - The converter object.
Examples:
  • The page numbering will start with 0. Set exclude_header_on_pages to "1" and the page numbering will start on the second page with 1.
    setPageNumberingOffset(1)
  • The page numbering will start with 11 on the first page. It can be useful for joining documents.
    setPageNumberingOffset(-10)

 

General Options

def setNoBackground(no_background)
Do not print the background graphics.
Parameter Description Default
no_background
Set to true to disable the background graphics.
false
Returns
  • HtmlToPdfClient - The converter object.
def setDisableJavascript(disable_javascript)
Do not execute JavaScript.
Parameter Description Default
disable_javascript
Set to true to disable JavaScript in web pages.
false
Returns
  • HtmlToPdfClient - The converter object.
def setDisableImageLoading(disable_image_loading)
Do not load images.
Parameter Description Default
disable_image_loading
Set to true to disable loading of images.
false
Returns
  • HtmlToPdfClient - The converter object.
def setDisableRemoteFonts(disable_remote_fonts)
Disable loading fonts from remote sources.
Parameter Description Default
disable_remote_fonts
Set to true disable loading remote fonts.
false
Returns
  • HtmlToPdfClient - The converter object.
def setBlockAds(block_ads)
Try to block ads. Enabling this option can produce smaller output and speed up the conversion.
Parameter Description Default
block_ads
Set to true to block ads in web pages.
false
Returns
  • HtmlToPdfClient - The converter object.
def setDefaultEncoding(default_encoding)
Set the default HTML content text encoding.
Parameter Description Default
default_encoding
The text encoding of the HTML content.
auto detect
Returns
  • HtmlToPdfClient - The converter object.
def setHttpAuth(user_name, password)
Set credentials to access HTTP base authentication protected websites.
Parameter Description Default
user_name
Set the HTTP authentication user name.
password
Set the HTTP authentication password.
Returns
  • HtmlToPdfClient - The converter object.
def setUsePrintMedia(use_print_media)
Use the print version of the page if available (@media print).
Parameter Description Default
use_print_media
Set to true to use the print version of the page.
false
Returns
  • HtmlToPdfClient - The converter object.
def setNoXpdfcrowdHeader(no_xpdfcrowd_header)
Do not send the X-Pdfcrowd HTTP header in Pdfcrowd HTTP requests.
Parameter Description Default
no_xpdfcrowd_header
Set to true to disable sending X-Pdfcrowd HTTP header.
false
Returns
  • HtmlToPdfClient - The converter object.
def setCookies(cookies)
Set cookies that are sent in Pdfcrowd HTTP requests.
Parameter Description Default
cookies
The cookie string.
Returns
  • HtmlToPdfClient - The converter object.
Examples:
  • setCookies("session=6d7184b3bf35;token=2710")
def setVerifySslCertificates(verify_ssl_certificates)
Do not allow insecure HTTPS connections.
Parameter Description Default
verify_ssl_certificates
Set to true to enable SSL certificate verification.
false
Returns
  • HtmlToPdfClient - The converter object.
def setFailOnMainUrlError(fail_on_error)
Abort the conversion if the main URL HTTP status code is greater than or equal to 400.
Parameter Description Default
fail_on_error
Set to true to abort the conversion.
false
Returns
  • HtmlToPdfClient - The converter object.
def setFailOnAnyUrlError(fail_on_error)
Abort the conversion if any of the sub-request HTTP status code is greater than or equal to 400.
Parameter Description Default
fail_on_error
Set to true to abort the conversion.
false
Returns
  • HtmlToPdfClient - The converter object.
def setCustomJavascript(custom_javascript)
Run a custom JavaScript after the document is loaded. The script is intended for post-load DOM manipulation (add/remove elements, update CSS, ...).
Parameter Description Default
custom_javascript
String containing a JavaScript code.
The string must not be empty.
Returns
  • HtmlToPdfClient - The converter object.
def setCustomHttpHeader(custom_http_header)
Set a custom HTTP header that is sent in Pdfcrowd HTTP requests.
Parameter Description Default
custom_http_header
A string containing the header name and value separated by a colon.
Returns
  • HtmlToPdfClient - The converter object.
Examples:
  • setCustomHttpHeader("X-My-Client-ID:k2017-12345")
def setJavascriptDelay(javascript_delay)
Wait the specified number of milliseconds to finish all JavaScript after the document is loaded. The maximum value is determined by your API license.
Parameter Description Default
javascript_delay
The number of milliseconds to wait.
Must be a positive integer number or 0.
200
Returns
  • HtmlToPdfClient - The converter object.
def setElementToConvert(selectors)
Convert only the specified element from the main document and its children. The element is specified by one or more CSS selectors. If the element is not found, the conversion fails. If multiple elements are found, the first one is used.
Parameter Description Default
selectors
One or more CSS selectors separated by commas.
The string must not be empty.
Returns
  • HtmlToPdfClient - The converter object.
Examples:
  • The first element with the id main-content is converted.
    setElementToConvert("#main-content")
  • The first element with the class name main-content is converted.
    setElementToConvert(".main-content")
  • The first element with the tag name table is converted.
    setElementToConvert("table")
  • The first element with the tag name table or with the id main-content is converted.
    setElementToConvert("table, #main-content")
  • The first element <p class="article"> within <div class="user-panel main"> is converted.
    setElementToConvert("div.user-panel.main p.article")
def setElementToConvertMode(mode)
Specify the DOM handling when only a part of the document is converted.
Parameter Description Default
mode
Allowed values:
  • cut-out
    The element and its children are cut out of the document.
  • remove-siblings
    All element's siblings are removed.
  • hide-siblings
    All element's sibilings are hidden.
cut-out
Returns
  • HtmlToPdfClient - The converter object.
def setWaitForElement(selectors)
Wait for the specified element in a source document. The element is specified by one or more CSS selectors. The element is searched for in the main document and all iframes. If the element is not found, the conversion fails.
Parameter Description Default
selectors
One or more CSS selectors separated by commas.
The string must not be empty.
Returns
  • HtmlToPdfClient - The converter object.
Examples:
  • Wait until an element with the id main-content is found.
    setWaitForElement("#main-content")
  • Wait until an element with the class name main-content is found.
    setWaitForElement(".main-content")
  • Wait until an element with the tag name table is found.
    setWaitForElement("table")
  • Wait until an element with the tag name table or with the id main-content is found.
    setWaitForElement("table, #main-content")
  • Wait until <p class="article"> is found within <div class="user-panel main">.
    setWaitForElement("div.user-panel.main p.article")

 

Print Resolution

def setViewport(width, height)
Set the viewport size. The viewport is the user's visible area of the page.
Parameter Description Default
width
Set the viewport width in pixels. The viewport is the user's visible area of the page.
The value must be in a range 96-7680.
1024
height
Set the viewport height in pixels. The viewport is the user's visible area of the page.
Must be a positive integer number.
768
Returns
  • HtmlToPdfClient - The converter object.
def setRenderingMode(rendering_mode)
Sets the rendering mode.
Parameter Description Default
rendering_mode
The rendering mode.
Allowed values:
  • default
    This mode is compatible with the Chrome preview.
  • viewport
    Takes the viewport width into account.
default
Returns
  • HtmlToPdfClient - The converter object.
def setScaleFactor(scale_factor)
Set the scaling factor (zoom) for the main page area.
Parameter Description Default
scale_factor
The scale factor.
The value must be in a range 10-500.
100
Returns
  • HtmlToPdfClient - The converter object.
Set the scaling factor (zoom) for the header and footer.
Parameter Description Default
Returns
  • HtmlToPdfClient - The converter object.
def setDisableSmartShrinking(disable_smart_shrinking)
Disable the intelligent shrinking strategy that tries to optimally fit the HTML contents to a PDF page.
Parameter Description Default
disable_smart_shrinking
Set to true to disable the intelligent shrinking strategy.
false
Returns
  • HtmlToPdfClient - The converter object.

 

PDF Format

Miscellaneous values for PDF output.

def setLinearize(linearize)
Create linearized PDF. This is also known as Fast Web View.
Parameter Description Default
linearize
Set to true to create linearized PDF.
false
Returns
  • HtmlToPdfClient - The converter object.
def setEncrypt(encrypt)
Encrypt the PDF. This prevents search engines from indexing the contents.
Parameter Description Default
encrypt
Set to true to enable PDF encryption.
false
Returns
  • HtmlToPdfClient - The converter object.
def setUserPassword(user_password)
Protect the PDF with a user password. When a PDF has a user password, it must be supplied in order to view the document and to perform operations allowed by the access permissions.
Parameter Description Default
user_password
The user password.
Returns
  • HtmlToPdfClient - The converter object.
def setOwnerPassword(owner_password)
Protect the PDF with an owner password. Supplying an owner password grants unlimited access to the PDF including changing the passwords and access permissions.
Parameter Description Default
owner_password
The owner password.
Returns
  • HtmlToPdfClient - The converter object.
def setNoPrint(no_print)
Disallow printing of the output PDF.
Parameter Description Default
no_print
Set to true to set the no-print flag in the output PDF.
false
Returns
  • HtmlToPdfClient - The converter object.
def setNoModify(no_modify)
Disallow modification of the ouput PDF.
Parameter Description Default
no_modify
Set to true to set the read-only only flag in the output PDF.
false
Returns
  • HtmlToPdfClient - The converter object.
def setNoCopy(no_copy)
Disallow text and graphics extraction from the output PDF.
Parameter Description Default
no_copy
Set to true to set the no-copy flag in the output PDF.
false
Returns
  • HtmlToPdfClient - The converter object.
def setTitle(title)
Set the title of the PDF.
Parameter Description Default
title
The title.
Returns
  • HtmlToPdfClient - The converter object.
def setSubject(subject)
Set the subject of the PDF.
Parameter Description Default
subject
The subject.
Returns
  • HtmlToPdfClient - The converter object.
def setAuthor(author)
Set the author of the PDF.
Parameter Description Default
author
The author.
Returns
  • HtmlToPdfClient - The converter object.
def setKeywords(keywords)
Associate keywords with the document.
Parameter Description Default
keywords
The string with the keywords.
Returns
  • HtmlToPdfClient - The converter object.

 

Viewer Preferences

These preferences specify how a PDF viewer should present the document. The preferences may be ignored by some PDF viewers.

def setPageLayout(page_layout)
Specify the page layout to be used when the document is opened.
Parameter Description Default
page_layout
Allowed values:
  • single-page
    Display one page at a time.
  • one-column
    Display the pages in one column.
  • two-column-left
    Display the pages in two columns, with odd-numbered pages on the left.
  • two-column-right
    Display the pages in two columns, with odd-numbered pages on the right.
Returns
  • HtmlToPdfClient - The converter object.
def setPageMode(page_mode)
Specify how the document should be displayed when opened.
Parameter Description Default
page_mode
Allowed values:
  • full-screen
    Full-screen mode.
  • thumbnails
    Thumbnail images are visible.
  • outlines
    Document outline is visible.
Returns
  • HtmlToPdfClient - The converter object.
def setInitialZoomType(initial_zoom_type)
Specify how the page should be displayed when opened.
Parameter Description Default
initial_zoom_type
Allowed values:
  • fit-width
    The page content is magnified just enough to fit the entire width of the page within the window.
  • fit-height
    The page content is magnified just enough to fit the entire height of the page within the window.
  • fit-page
    The page content is magnified just enough to fit the entire page within the window both horizontally and vertically. If the required horizontal and vertical magnification factors are different, use the smaller of the two, centering the page within the window in the other dimension.
Returns
  • HtmlToPdfClient - The converter object.
def setInitialPage(initial_page)
Display the specified page when the document is opened.
Parameter Description Default
initial_page
Must be a positive integer number.
Returns
  • HtmlToPdfClient - The converter object.
def setInitialZoom(initial_zoom)
Specify the initial page zoom in percents when the document is opened.
Parameter Description Default
initial_zoom
Must be a positive integer number.
Returns
  • HtmlToPdfClient - The converter object.
def setHideToolbar(hide_toolbar)
Specify whether to hide the viewer application's tool bars when the document is active.
Parameter Description Default
hide_toolbar
Set to true to hide tool bars.
false
Returns
  • HtmlToPdfClient - The converter object.
def setHideMenubar(hide_menubar)
Specify whether to hide the viewer application's menu bar when the document is active.
Parameter Description Default
hide_menubar
Set to true to hide the menu bar.
false
Returns
  • HtmlToPdfClient - The converter object.
def setHideWindowUi(hide_window_ui)
Specify whether to hide user interface elements in the document's window (such as scroll bars and navigation controls), leaving only the document's contents displayed.
Parameter Description Default
hide_window_ui
Set to true to hide ui elements.
false
Returns
  • HtmlToPdfClient - The converter object.
def setFitWindow(fit_window)
Specify whether to resize the document's window to fit the size of the first displayed page.
Parameter Description Default
fit_window
Set to true to resize the window.
false
Returns
  • HtmlToPdfClient - The converter object.
def setCenterWindow(center_window)
Specify whether to position the document's window in the center of the screen.
Parameter Description Default
center_window
Set to true to center the window.
false
Returns
  • HtmlToPdfClient - The converter object.
def setDisplayTitle(display_title)
Specify whether the window's title bar should display the document title. If false , the title bar should instead display the name of the PDF file containing the document.
Parameter Description Default
display_title
Set to true to display the title.
false
Returns
  • HtmlToPdfClient - The converter object.
def setRightToLeft(right_to_left)
Set the predominant reading order for text to right-to-left. This option has no direct effect on the document's contents or page numbering but can be used to determine the relative positioning of pages when displayed side by side or printed n-up
Parameter Description Default
right_to_left
Set to true to set right-to-left reading order.
false
Returns
  • HtmlToPdfClient - The converter object.

 

Miscellaneous

def setDebugLog(debug_log)
Turn on the debug logging. Details about the conversion are stored in the debug log. The URL of the log can be obtained from the getDebugLogUrl method.
Parameter Description Default
debug_log
Set to true to enable the debug logging.
false
Returns
  • HtmlToPdfClient - The converter object.
def getDebugLogUrl()
Get the URL of the debug log for the last conversion.
Returns
  • string - The link to the debug log.
def getRemainingCreditCount()
Get the number of conversion credits available in your account.
The returned value can differ from the actual count if you run parallel conversions.
The special value 999999 is returned if the information is not available.
Returns
  • int - The number of credits.
def getConsumedCreditCount()
Get the number of credits consumed by the last conversion.
Returns
  • int - The number of credits.
def getJobId()
Get the job id.
Returns
  • string - The unique job identifier.
def getPageCount()
Get the total number of pages in the output document.
Returns
  • int - The page count.
def getOutputSize()
Get the size of the output in bytes.
Returns
  • int - The count of bytes.

 

API Client Options

def setUseHttp(use_http)
Specifies if the client communicates over HTTP or HTTPS with Pdfcrowd API.
Parameter Description Default
use_http
Set to true to use HTTP.
false
Returns
  • HtmlToPdfClient - The converter object.
def setUserAgent(user_agent)
Set a custom user agent HTTP header. It can be usefull if you are behind some proxy or firewall.
Parameter Description Default
user_agent
The user agent string.
pdfcrowd_ruby_client/4.3.3 (http://pdfcrowd.com)
Returns
  • HtmlToPdfClient - The converter object.
def setProxy(host, port, user_name, password)
Specifies an HTTP proxy that the API client library will use to connect to the internet.
Parameter Description Default
host
The proxy hostname.
port
The proxy port.
user_name
The username.
password
The password.
Returns
  • HtmlToPdfClient - The converter object.
def setRetryCount(retry_count)
Specifies the number of retries when the 502 HTTP status code is received. The 502 status code indicates a temporary network issue. This feature can be disabled by setting to 0.
Parameter Description Default
retry_count
Number of retries wanted.
1
Returns
  • HtmlToPdfClient - The converter object.

 

Error Handling

begin 
    # call the API 
rescue Pdfcrowd::Error => why 
    # print error
    STDERR.puts "Pdfcrowd Error: #{why}"

    # print just error code
    STDERR.puts "Pdfcrowd Error Code: #{why.getCode()}"

    # print just error message
    STDERR.puts "Pdfcrowd Error Message: #{why.getMessage()}"

    # or handle the error by your way
end

Troubleshooting

  • Check API Status Codes in case of the error code is returned.
  • You can use setDebugLog and getDebugLogUrl to get detailed info about the conversion, such as conversion errors, time, console output.
  • You can use our JavaScript library to resolve rendering problems, such as missing content or blank pages.
    Just use setCustomJavascript with libPdfcrowd.highlightHtml(borders, backgrounds, labels, noZeroSpace) method call to visualize all HTML elements. See example.
  • Take a look at the FAQ section.