Convert DOCX to PDF or PDF to DOCX in Ruby

DOCX to PDF conversion

In modern days, many word document processing applications provide the capabilities to process MS Word and OpenOffice file formats and also, allow you to render the file into other file formats including PDF. So, this whole process incurs an effort to install an App as well as licensing cost. But why pay for the whole product licensing when you only need to perform one single operation and they also lack vice versa conversion feature of PDF to DOCX formats. Furthermore, bulk conversion operations cannot be performed with such software and in all such circumstances, Aspose.Words Cloud API comes into action.

We are going to discuss following topics in details

Word processing API

Aspose.Words Cloud is an exciting solution to create, manipulate and transform leading Word document formats including DOC, DOCX, DOCM, DOTX, RTF, ODT, OTT, etc. Perform all these operations in the cloud without MS Office automation or any other dependencies. Since it is developed as per REST API architecture, so it can be accessed on any platform. Furthermore, in order to facilitate our customers, specific programming language SDKs are developed so that you can access the API right within your code. In this article, our emphasis is on Aspose.Words Cloud SDK for Ruby enabling the Ruby developers to implement Word document processing capabilities in Ruby application.

Installation

In order to use the SDK, we need to first install it over the system. The SDK is available for download as Ruby GEM and GitHub. Once you have Ruby runtime configured, please execute the following command on the terminal to perform the installation.

gem install aspose_words_cloud

But before you proceed with the installation of Aspose.Words Cloud SDK for Ruby, you need to install the following dependencies.

Runtime dependencies(3)

Development dependencies (1)

Aspose.Cloud account subscription

In order to use the SDK, the next step is to obtain your personalised ClientID and ClientSecret details. Therefore, the first step is to create an account by visiting Aspose.Cloud dashboard. If you have GitHub or Google account, simply Sign Up. Otherwise, click on the Create a new Account button and provide the required information. Now login to the dashboard using credentials and expand the Applications section from the dashboard and scroll down towards the Client Credentials section to see Client ID and Client Secret details.

Client Credentials

DOCX to PDF conversion in Ruby

Please follow the instructions given below to perform the MS Word files (DOCX/DOC) to PDF conversion using Ruby language.

  • The first step is to create variables holding your ClientID and ClientSecret details from the dashboard
  • Secondly, create an instance of configure from AsposeWordsCloud and pass ClientID and ClientSecret values
  • Thirdly, create an instance of WordsAPI
  • Now upload source DOCX file to cloud storage using UploadFileRequest(…) method
  • The next step is to create an object of ConvertDocumentRequest(..) which takes input DOCX name, resultant format as arguments
  • Finally, call the convert_document(..) method to perform the conversion operation

PDF to DOCX conversion in Ruby

The DOCX, DOC, RTF, ODT, and PDF, are among the major document formats and are capable of encapsulating almost any type of data including text, tables, raster and vector graphics, video, audio, and also support a wide range of formatting features. However, one of the exciting features which outstands PDF is its capabilities to maintain document fidelity on any platform, so you may receive lots of document in PDF format. And in order to edit them, you need specific applications. However, Aspose.Words Cloud transforms a PDF to Word-based document format, that can be naturally edited with any mainstream word processor and you only pay for the requests you send to the server.

In this section, we are going to discuss the steps and related details for the conversion of PDF file to DOCX format.

  • First, we need to create an object of WordsAPI where we provide ClientID and ClientSecret details
  • Secondly, create an instance of SaveAsRequest which takes SaveOptionsData object specifying target format and output file name as arguments
  • Finally, call save_as(..) method and pass the SaveAsRequest object to perform the conversion

Use cURL command for DOC to PDF conversion

The cURL commands are an exciting way to access REST APIs on any platform. Since Aspose.Words Cloud is developed as per REST principles, so we can use them to perform the conversion operation. However, before proceeding, we need to first generate JSON Web Token (JWT) based on your ClientID and ClientSecret details retrieved from Aspose.Cloud dashboard. Please execute the following command in the terminal to generate the JWT token.

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=718e4235-8866-4ebe-bff4-f5a14a4b6466&client_secret=388e864b819d8b067a8b1cb625a2ea8e" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Once a new JWT token is generated, use the following command to convert the DOC file to PDF format. The name parameter indicates the input DOCX file already available on Cloud storage, the format parameter denotes the format of the resultant file and outPath shows the location for the resultant PDF document. The conversion is performed using GetDocumentWithFormat API and in the example given below, test_multi_pages.docx file is rendered to PDF format and saved with the name MyConverted.pdf.

curl -X GET "https://api.aspose.cloud/v4.0/words/test_multi_pages.docx?format=pdf&outPath=MyConverted.pdf" \
-H  "accept: application/octet-stream" \
-H  "Authorization: Bearer <JWT Token>"

PDF to DOC conversion using the cURL command

The conversion of PDF to DOC/DOCX and other supported Word document formats can be performed using the cURL command. For this purpose, we need to use ConvertDocument API.

curl -X PUT "https://api.aspose.cloud/v4.0/words/converted.pdf/saveAs" \
-H  "accept: application/json" \
-H  "Authorization: Bearer <JWT Token>" \
-H  "Content-Type: application/json" \
-d "{\"SaveFormat\":\"docx\",\"FileName\":\"output.docx\",\"AllowEmbeddingPostScriptFonts\":true,\"ZipOutput\":false,\"UpdateLastSavedTimeProperty\":true,\"UpdateSdtContent\":true,\"UpdateFields\":true,\"Dml3DEffectsRenderingMode\":\"Basic\",\"UpdateCreatedTimeProperty\":true,\"UpdateLastPrintedProperty\":true}"

Conclusion

In the above article, we have discussed the exciting and convenient way of DOCX file to PDF conversion and PDF to DOC/DOCX file conversion using Aspose.Words Cloud SDK for Ruby. Please note that all our Cloud SDKs are Opensource and similarly, the complete code of Aspose.Words cloud SDK for Ruby is available on GitHub.

Related links

We also recommend visiting following links for related details on