Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save groupdocs-cloud-gists/423249ef7f7e7f5b60f4ebd5e1dd79c7 to your computer and use it in GitHub Desktop.
Save groupdocs-cloud-gists/423249ef7f7e7f5b60f4ebd5e1dd79c7 to your computer and use it in GitHub Desktop.
How to Convert HTML to PDF in Ruby using Cloud REST API
# API initialization: Create an instance of the conversion APIs
@fileApi = GroupDocsConversionCloud::FileApi.from_keys(@client_id, @client_secret)
# Download PDF File Request
@request = GroupDocsConversionCloud::DownloadFileRequest.new("html-to-pdf/two-pages.pdf", @mystorage)
@response = @fileApi.download_file(@request)
puts("PDF file successfully downloaded." + (@response).to_s )

Learn how to convert HTML to PDF in Ruby using REST API:

The following topics shall be covered in this article:

HTML to PDF Conversion REST API and Ruby SDK Installation Convert HTML to PDF in Ruby using REST API HTML to PDF Conversion using Advanced Options in Ruby Online HTML to PDF Converter for Free

# Create instance of the HTML to PDF conversion API
@fileApi = GroupDocsConversionCloud::ConvertApi.from_keys(@client_id, @client_secret)
# Set the convert settings for advanced file options
settings = GroupDocsConversionCloud::ConvertSettings.new
settings.file_path = "html-to-pdf/two-pages.html"
settings.format = "pdf"
loadOptions = GroupDocsConversionCloud::HtmlLoadOptions.new
loadOptions.page_numbering = true
convertOptions = GroupDocsConversionCloud::PdfConvertOptions.new
convertOptions.center_window = true
convertOptions.compress_images = false
convertOptions.display_doc_title = true
convertOptions.dpi = 1024.0
convertOptions.fit_window = false
convertOptions.from_page = 1
convertOptions.grayscale = false
convertOptions.image_quality = 100
convertOptions.linearize = false
convertOptions.margin_top = 5
convertOptions.margin_left = 5
convertOptions.password = "password"
convertOptions.unembed_fonts = true
convertOptions.remove_unused_streams = true
convertOptions.remove_unused_objects = true
convertOptions.remove_pdfa_compliance = false
settings.load_options = loadOptions
settings.convert_options = convertOptions
settings.output_path = "html-to-pdf"
# Convert HTML to PDF
result = @fileApi.convert_document(GroupDocsConversionCloud::ConvertDocumentRequest.new(settings))
puts("HTML file successfully converted to pdf file with advanced file options.")
# Create necessary API instances
@fileApi = GroupDocsConversionCloud::ConvertApi.from_keys(@client_id, @client_secret)
# Prepare convert settings
settings = GroupDocsConversionCloud::ConvertSettings.new
settings.file_path = "html-to-pdf/two-pages.html"
settings.format = "pdf"
loadOptions = GroupDocsConversionCloud::HtmlLoadOptions.new
loadOptions.page_numbering = true
settings.load_options = loadOptions
settings.output_path = "html-to-pdf"
# Convert to PDF
result = @fileApi.convert_document(GroupDocsConversionCloud::ConvertDocumentRequest.new(settings))
puts("HTML file successfully converted to pdf file.")
# API initialization to create an instance of the conversion APIs
@fileApi = GroupDocsConversionCloud::FileApi.from_keys(@client_id, @client_secret)
# Upload file to cloud storage
@htmlfile = "#{Rails.root}/public/groupdocs-files/two-pages.html"
@fileStream = File.new(@htmlfile, "r")
@request = GroupDocsConversionCloud::UploadFileRequest.new("html-to-pdf/two-pages.html", @fileStream, @mystorage)
@response = @fileApi.upload_file(@request)
@fileStream.close()
puts("HTML File Uploading completed." + (@response).to_s )
# Get your client_id and client_secret from https://dashboard.groupdocs.cloud after free registration.
# Load the gem in your ruby application for http://api.groupdocs.cloud
require 'groupdocs_conversion_cloud'
@client_id = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
@client_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# Add your storage name here
@mystorage = "InternalStorage"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment