This is the documentation of the Ruby client library for the Pdfcrowd HTML to PDF API. Here are some sample PDFs generated by the API:
|
|
|
|
|
| Status report | JavaScript vector chart | Wikipedia page | Invoice | Newsletter |
Integration with your application is a matter of a few minutes:
$ gem install pdfcrowdOr download pdfcrowd-2.4.0.gem and run
$ gem install pdfcrowd-2.4.0.gem
You can also check out the source on Github: pdfcrowd-ruby
In the following examples, do not forget to replace "username" and "apikey" with your username and API key.
The following code shows how to convert a web page, raw HTML code, and a local HTML file:
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
The following code shows how to convert a web page in a Ruby on Rails controller method:
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
You can also convert raw HTML code, just use the convertHtml() method instead of convertURI():
pdf = client.convertHtml("<head></head><body>My HTML Layout</body>")
The API lets you also convert a local HTML file:
pdf = client.convertFile("/path/to/MyLayout.html")
begin # .. # call the API rescue Pdfcrowd::Error => why # handle the error end
Provides access to the Pdfcrowd API from your Ruby applications.
def initialize(username, apikey)
def convertHtml(html, outstream=nil)
write(str) method.
If outstream is not provided then the return value is a string
containing the created PDF.
def convertFile(fpath, outstream=nil)
write(str) method.
If outstream is not provided then the return value is a string
containing the created PDF.
def convertURI(url, outstream=nil)
write(str) method.
If outstream is not provided then the return value is a string
containing the created PDF.
def setPageWidth(value)
def setPageHeight(value)
def setPageMargins(top, right, bottom, left)
def setHorizontalMargin(value)
def setVerticalMargin(value)
def setFooterHtml(html)
def setFooterUrl(url)
def setHeaderHtml(html)
def setHeaderUrl(url)
def setHeaderFooterPageExcludeList(exclist)
def setPageNumberingOffset(offset)
def enableImages(value)
def enableBackgrounds(value)
def setHtmlZoom(value)
def enableJavaScript(value)
def enableHyperlinks(value)
def setDefaultTextEncoding(value)
utf-8.
def usePrintMedia(value)
print CSS media type
is used (if available).
def setEncrypted(value)
def setUserPassword(pwd)
def setOwnerPassword(pwd)
def setNoPrint(value)
def setNoModify(value)
def setNoCopy(value)
def setPageLayout(value)
SINGLE_PAGECONTINUOUSCONTINUOUS_FACINGdef setPageMode(value)
FULLSCREEN - Full-screen mode.def setInitialPdfZoomType(value)
FIT_WIDTHFIT_HEIGHTFIT_PAGEdef setInitialPdfExactZoom(value)
def setPdfScalingFactor(value)
def setPageBackgroundColor(value)
def setTransparentBackground(value)
body {background-color:rgba(255,255,255,0.0);}
def setAuthor(author)
def setWatermark(url, offset_x=0, offset_y=0)
def setWatermarkRotation(angle)
def setWatermarkInBackground(value)
def useSSL(use_ssl)
def numTokens()
def setMaxPages(npages)
def setFailOnNon200(value)
Derived from RuntimeError. It is thrown when an error
occurs. Use to_s() method to get the error message.
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".