Convert Word Documents to PDF in Ruby using REST API

Convert Word Documents to PDF in Ruby using REST API
How to Convert Word Documents to PDF using Ruby

DOCX is one of the most popular word processors in the world. However, MS DOCX reformats documents and can be altered when opened on a different computer system. While PDF files are mobile device-friendly, easy to read and can’t be altered. That’s why users convert word documents to PDF in ruby when sending important information like online bills, transactions history, and handouts etc.

Now you know why you should convert Word documents to PDF file. You can convert Word DOCX to PDF using the built-in functionality provided by Microsoft Office, but you may need to convert your Word documents DOCX to PDF programmatically. Using Groupdocs Conversion APIs to convert your Word documents to PDF is fast, easy and instant. All you need is a stable internet connection and your files. In this article, we will learn how to convert Word documents to PDF in Ruby using REST API.

The following topics shall be covered in this blog article:

Word to PDF Conversion REST API – Ruby API Installations

You can easily convert DOCX files to PDF using the Ruby SDK of GroupDocs.Conversion Cloud. It helps you to quickly and reliably convert documents of supported file formats to other document formats – just in a few seconds and in high quality. GroupDocs.Conversion Cloud REST API allows you to convert documents across a wide range of supported file formats without any dependency software. It is compatible will all major office software and is completely independent of operating system.

GroupDocs.Conversion provides high quality document conversion solutions. You can check our available SDKs list here to transform documents to a new format using our cloud REST APIs. You can also call this REST APIs directly from your browser with GroupDocs.Conversion Cloud API reference Swagger UI. A gem groupdocs_conversion_cloud is available at rubygems. You can install GroupDocs.Conversion Cloud API to convert word documents to PDF in Ruby application using this gem with following command in the rails console:

gem install groupdocs_conversion_cloud

Addressable ~> 2.5.0, >= 2.5.0 is runtime dependency gem for groupdocs_conversion_cloud. You can also copy conversion gem into your Gemfile for communicating with the GroupDocs.Conversion Cloud API and then run bundle install:

gem "groupdocs_conversion_cloud", "~> 22.3"
bundle install

Next, you need to add a Client Id and a Client Secret before making any requests to GroupDocs Conversion Cloud API. This will be used to call on GroupDocs Cloud API into your existing project. You can get client credentials by creating a new Application on the Groupdocs dashboard. Once you have Client Id and a Client Secret, add these in the ruby code snippet as shown below:

Convert Word Documents to PDF in Ruby using REST API

You can convert Word documents to PDF programmatically on the cloud by following the simple steps as given below:

  1. Upload the DOCX file to the cloud
  2. Convert DOCX to PDF file
  3. Download the converted PDF file
  4. Delete file from the cloud storage

1. Upload the DOCX File

First, upload the DOCX file to the cloud storage using the below code sample:

As a result, the uploaded DOCX file will be available in the files section of your dashboard on the cloud.

2. Convert DOCX to PDF using Ruby

You can convert DOCX to PDF file programmatically by following the steps mentioned below:

  • First of all, create an instance of the FileApi.
  • Next, create an instance of the ConvertSettings.
  • Then, set the input DOCX file path.
  • Provide format for the output file as the “pdf”.
  • Now, provide the output directory path as “conversion”.
  • Next, create ConvertDocumentRequest with provided settings.
  • Finally, call convert_document() method with ConvertDocumentRequest as an argument.

The following code snippet shows how to convert DOCX to PDF using REST API in Ruby.

Converted DOCX to PDF using Ruby
Convert DOCX to PDF using Ruby

3. Download the Converted File

The converted PDF file has been saved on the cloud. The following code snippet demonstrates how to download a file using Ruby:

4. Delete File from Cloud Storage

You may delete the converted PDF file using the code sample as show below:

Word to PDF Conversion with Advanced Options in Ruby

You can also convert Word documents to PDF files in ruby with advance options by following the steps as shown below:

  • First, create an instance of the FileApi.
  • Now, create ConvertDocumentRequest with ConvertSettings
  • Then, set the input DOCX file path.
  • Provide format of the resultant file as “pdf”.
  • Now, create an instance of the DocxLoadOptions.
  • Set password for loadOptions
  • Next, create an instance of the PdfConvertOptions
  • Then, set various convert options such as center_window, display_doc_title, margin, image_quality and fonts etc.
  • Provide load_options, convert_options and output_path settings.
  • Finally, convert DOCX by calling the convert_document() method with ConvertDocumentRequest

The following code sample shows how to convert a Word document to a PDF document with advanced convert options. Please follow the steps mentioned earlier to upload and download a file.

As a result, Word DOCX file will be converted into PDF file using advance file options.

Free DOCX to PDF Converter Online

How to convert Word to PDF online? Our free and online PDF converter will create PDFs from your Word documents. This online PDF converter is developed using the Groupdocs Conversion API and preserve the layout of your file. Convert documents DOCX to PDF free exactly as the original PDF file.

Conclusion

In this article, we have learned how to convert Word documents to PDF files on the cloud. You have also seen how to convert DOCX to PDF with advance options using Ruby. This article also explained how to programmatically upload the DOCX file on the cloud and then download the converted PDF file from the cloud. Besides, you can learn more about GroupDocs.Conversion Cloud API using the documentation. We also provide an API Reference section that lets you visualize and interact with our APIs directly through the browser.

Ask a question

If you have any queries about DOCX to PDF converter, please feel free to ask us on the Forum.

See Also

We recommend following related link of supported document conversions: