htmltopdf

Create PDF file from provided HTML or URL.

Ruby:

      
@hypdf_response = HyPDF.htmltopdf(
  '<html><body><h1>Title</h1></body></html>',
  user: 'YOUR_HYPDF_USER',
  password: 'YOUR_HYPDF_PASSWORD',
  # ... other options ...
)
# => {pdf: BINARY_PDF_DATA, pages: 1, page_size: "792 x 612 pts (letter)", pdf_version: 1.4}
      
    

In other languages just make HTTP request with these parameters:

      
URL: 'https://www.hypdf.com/htmltopdf'
Method: 'POST'
Content-Type: 'application/json'
Parameters: {content: 'YOUR_HTML', user: 'YOUR_HYPDF_USER', password: 'YOUR_HYPDF_PASSWORD'}
Result: 'BINARY_PDF_DATA'
      
    

Optional parameters can be passed directly or through a special meta tag in the HTML content:

<meta name="hypdf-option_name" content="value" />

Optional parameters:

Option name Possible value Description
test true, false Use test mode.
bucket String Upload created PDF to this AWS S3 bucket.
key String File name.
public true, false Grant everyone read access to uploaded file.
preview true, false Generate preview image for uploaded file.
callback String (URL) Create PDF in the background and post results to this URL.
disable_links true Render plain text instead of links.
disable_forms true Do not turn HTML form fields into pdf form fields.
disable_javascript true Do not allow web pages to run javascript
no_collate true Do not collate when printing multiple copies
copies Integer Number of copies to print into the pdf file (default 1)
grayscale true PDF will be generated in grayscale
lowquality true Generates lower quality pdf/ps. Useful to shrink the result document space
image_dpi Integer When embedding images scale them down to this dpi (default 600)
image_quality Integer When jpeg compressing images use this quality (default 94)
margin_bottom String ("0.5in", "15mm", "2cm" etc) Set the page bottom margin (default "0.75in")
margin_left String ("0.5in", "15mm", "2cm" etc) Set the page left margin (default "0.75in")
margin_right String ("0.5in", "15mm", "2cm" etc) Set the page right margin (default "0.75in")
margin_top String ("0.5in", "15mm", "2cm" etc) Set the page top margin (default "0.75in")
orientation Landscape, Portrait Set orientation (default "Portrait")
page_height String ("8.27in", "297mm", "21cm" etc) Page height
page_width String ("8.27in", "297mm", "21cm" etc) Page width
page_size A0..B10, C5E, Comm10E, DLE, Executive, Folio, Ledger, Legal, Letter, Tabloid Set paper size (default "Letter")
title String The title of the generated pdf file (The title of the first document is used if not specified)
no_background true Do not print background
cookie String ("name value") Set an additional cookie (repeatable)
custom_header String ("name value") Set an additional HTTP header (repeatable)
no_custom_header_propagation true Do not add HTTP headers specified by custom_header for each resource request.
encoding String Set the default text encoding, for input (default "UTF-8")
no_images true Do not load or print images
javascript_delay Integer Wait some milliseconds for javascript finish (default 200)
no_stop_slow_scripts true Do not Stop slow running javascripts
run_script String (code) Run this additional javascript after the page is done loading (repeatable)
minimum_font_size Integer Minimum font size
page_offset Integer Set the starting page number (default 0)
user_style_sheet String Specify a URL to user style sheet, to load with every page
window_status String Wait until window.status is equal to this string before rendering page
zoom Float Use this zoom factor (default 1)
header_left String Left aligned header text.
header_right String Right aligned header text.
header_center String Centered header text.
header_font_name String Set header font name (default Arial).
header_font_size Integer Set header font size (default 12).
header_line true Display line below the header.
header_html String (URL) Adds a html header (overrides any other header_* options listed above).
header_spacing Float Spacing between header and content in mm (default 0).
footer_left String Left aligned footer text.
footer_right String Right aligned footer text.
footer_center String Centered footer text.
footer_font_name String Set footer font name (default Arial).
footer_font_size Integer Set footer font size (default 12).
footer_line true Display line above the footer.
footer_html String (URL) Adds a html footer (overrides any other footer_* options listed above).
footer_spacing Float Spacing between content and footer in mm (default 0).

Also you can add the page numbering by using special snippets in the header/footer options. For example:

      
@hypdf_response = HyPDF.htmltopdf(
  '<html><body><h1>Title</h1></body></html>',
  user: 'YOUR_HYPDF_USER',
  password: 'YOUR_HYPDF_PASSWORD',
  footer_center: 'Page [page] from [toPage]'
)
      
    

Test mode

During the development you may want to use the test mode. In this case HyPDF do not count against your daily document quota.

      
@hypdf_response = HyPDF.pdfunite(
  '/path/to/file1.pdf',
  '/path/to/file2.pdf',
  user: 'YOUR_HYPDF_USER',
  password: 'YOUR_HYPDF_PASSWORD',
  test: true
)
      
    

Uploading to AWS S3

HyPDF can upload created files to your AWS S3 bucket, but before you need to give permission HyPDF to do it.

  • Visit your AWS Security Credentials page
  • Copy Canonical User ID from “Account Identifiers” section
  • Add it to your HyPDF account via add-on dashboard
  • Log into AWS Console
  • Choose S3 service
  • Select the bucket and click on "Permissions" link
  • On "Permissions" panel click on "Bucket policy" button
  • Paste the following policy into the dialog that appears (Replace YOUR_BUCKET_NAME with the name of the bucket you are adding this permission to)

Policy:

      
{
"Version": "2008-10-17",
"Statement": [
  {
    "Sid": "AddCannedAcl",
    "Effect": "Allow",
    "Principal": {
      "AWS": "arn:aws:iam::495273932038:root"
    },
    "Action": [
      "s3:GetBucketAcl",
      "s3:PutObjectAcl",
      "s3:PutObject"
    ],
    "Resource": [
      "arn:aws:s3:::YOUR_BUCKET_NAME/*",
      "arn:aws:s3:::YOUR_BUCKET_NAME"
    ]
  }
]
}
      
    

After that you can use the bucket option in HyPDF methods:

      
@hypdf_response = HyPDF.htmltopdf(
  '<html><body><h1>Title</h1></body></html>',
  user: 'YOUR_HYPDF_USER',
  password: 'YOUR_HYPDF_PASSWORD',
  # ... other options ...
  bucket: 'YOUR_BUCKET_NAME',
  key: 'some_file_name.pdf', # optional
  public: true # optional, generate public readable URL
)
# => { url: "https://s3.amazonaws.com/YOUR_BUCKET_NAME/some_file_name.pdf", pages: 1, page_size: "792 x 612 pts (letter)", pdf_version: 1.4, bucket: "YOUR_BUCKET_NAME", key: "some_file_name.pdf"}
      
    

Asynchronous creation

You don’t need to wait and block your application until the document is created, HyPDF can handle your files in asynchronous style. Just provide callback URL that will be called when PDF will be ready.

      
def create
  @hypdf_response = HyPDF.htmltopdf(
      '<html><body><h1>Title</h1></body></html>',
      user: 'YOUR_HYPDF_USER',
      password: 'YOUR_HYPDF_PASSWORD',
      # ... other options ...
      callback: 'http://www.your-application.com/save_pdf_url',
      bucket: 'YOUR_BUCKET_NAME',
      key: 'some_file_name.pdf',
      public: true
  )

  # You can check job status in any time
  puts HyPDF.jobstatus(@hypdf_response[:job_id])
end

# POST /save_pdf_url
def save_pdf_url
  if params[:error]
      puts params[:message]
  else
      # PDF url
      puts params[:url]
      # Job ID
      puts request.headers['hypdf-job-id']
      # Number of pages
      puts request.headers['hypdf-pages']
  end
end
      
    

Questions?

All HyPDF support and runtime issues or product feedback are welcome at support@hypdf.com. Also feel free to subscribe to our Twitter account.