Convert Word to PDF in Dart

Word to PDF
Convert Word to PDF online

Word documents (DOC/DOCX) are popular for office information storage and sharing and are used by Microsoft Word, OpenOffice, and other applications. They contain formatted text, images, tables, graphs, charts, page formatting, and print settings. However, PDF format is even widely accepted because most modern web browsers do support the feature to display PDF files.

In this article, we are going to discuss the steps on how to convert the Word documents to PDF format without MS Office automation or Adobe Acrobat. The complete conversion is performed using Free Dart SDK.

Word to PDF Conversion API

Aspose.Words Cloud is our award-winning API to create, edit as well as render them to PDF and other supported file formats. You may also work with elements like bookmarks, comments, page numbering, tables, footnotes, fonts, fields, drawing objects, header & footer, etc. Now as per the scope of this article, we are going to Aspose.Words Cloud SDK for Dart.

Installation

The first step is to download and install the Dart environment. The next step is to download the SDK which is available over Flutter and GitHub. However, we are going to focus on Aspose.Words Cloud SDK for Dart that is compatible with Flutter.

Create a Flutter project in your favorite IDE (in our case, we are using Android Studio) and then import the Dart SDK into your project by adding aspose_words_cloud dependency in pubspec.yaml.

Add Aspose.Words references
image 1: Adding Aspose.Words Cloud SDK for Dart dependencies.

The next important step is the creation of a Free account on Aspose.Cloud dashboard. If you have GitHub or Google account, simply Sign Up or simply click on the create a new Account button. Now obtain your personalized client credentials from the dashboard.

Word to PDF using Dart

Once we have set up the environment, we are ready to write the code for Word document conversion to PDF format. Please follow the steps defined below to accomplish this requirement.

  • First of all, we need to add references for the required libraries
  • Secondly, the next step is to define variables holding Client ID and Client Secret details (as per dashboard)
  • Thirdly, create an object of Configuration which takes Client ID and Client Secret details as arguments
  • Now next step is to create an object of WordsApi class that takes the Configuration instance as arguments
  • The next step is to read the content of the input Word document into the File instance
  • In order to upload the file to cloud storage, please use uploadFile(…) method of WordsApi
  • Now create an instance of SaveOptionsData class where we define the resultant format as PDF and output file name
  • Penultimate, create an instance of SaveAsRequest class which takes Word document name and SaveOptionsData object as arguments
  • Finally, call the saveAs(…) method of WordsApi to perform the conversion
Aspose.Words Cloud API
image 3: Preview of iOS App for Word to PDF conversion.

Convert DOC to PDF using the cURL command

Owing to the REST architecture of Aspose.Words Cloud API, it can be accessed on any platform using the cURL commands. Since APIs are only accessible to authorized users, so we need to generate a JWT access token based on Client ID and Client Secret details as specified on Aspose.Cloud dashboard. Please execute the following command to generate the JWT access token.

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=b1a1b925-cbd0-40c3-b7d5-075c93601243&client_secret=343ebf767f3f53537a45ced31d6be34f" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Now execute the following cURL command to upload a file from the local system to Cloud storage.

curl -X PUT "https://api.aspose.cloud/v4.0/words/storage/file/fileStoredInCloud.docx" \
-H  "accept: application/json" \
-H  "Authorization: Bearer <JWT Token>" \
-H  "Content-Type: multipart/form-data" \
-d {"fileContent":{}}

Now execute the following cURL command to convert the Word document available in Cloud storage to PDF format and save the output in the same Cloud storage.

curl -X PUT "https://api.aspose.cloud/v4.0/words/fileStoredInCloud.docx/saveAs" \
-H  "accept: application/json" \
-H  "Authorization: Bearer <JWT Token>" \
-H  "Content-Type: application/json" \
-d "{\"SaveFormat\":\"pdf\",\"FileName\":\"converted.pdf\"}"

Conclusion

We just have learned about the amazing capabilities of converting Word to PDF with fewer code lines. The SDK is so amazing that you only need to focus on your application’s business logic and leave the complex Word document processing. So this single SDK enables you to generate iOS and Android Apps which are capable of performing word document conversion to PDF format. We also have discussed the leverage of accessing the REST API through the cURL commands.

Since the SDK is open-source, so the complete source code is available for download over GitHub.

Related articles

We also recommend you visit the following links to learn more about the capabilities of our other APIs