Convert Word to PDF using Dart

Word to PDF conversion

Word documents are popular for office information storage and sharing. The DOC/DOCX are the widely used file formats used by Microsoft Word as well as by most word processing software, including OpenOffice. DOC stands for DOCument file and may contain formatted text, images, tables, graphs, charts, page formatting, and print settings. However, in order to view these documents, you need to have one of these softwares installed. One of the viable solutions is to convert Word documents to PDF format 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 processing API

Aspose.Words Cloud is our award-winning API to create, edit as well as render them to PDF and other Supported File Formats. The API is developed as per REST architecture, therefore it can be accessed on any platform such as Desktop or mobile. Furthermore, many complex operations like manipulation of bookmarks, comments, page numbering, tables, footnotes, fonts, fields, drawing objects, header & footer, etc can easily be performed with fewer code lines.

Furthermore, we also have created programming SDKs (a wrapper around Cloud API), that provide a language-level interface to access Aspose.Words Cloud API so that you can utilize the corresponding document-processing functionality in a manner consistent with language standards. In order to incorporate Word document processing capabilities in Dart, we have created Aspose.Words Cloud SDK for Dart.

Installation

In order to use the Dart SDK, you need to first 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.

Data integrity and privacy have trivial importance at Aspose. Therefore, the API is only accessible to authorized users only, so we need to create a Free account subscription on 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
image 2: Client Credentials on Aspose.cloud 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 of 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 which takes Configuration instance as argument
  • The next step is to read the content of input Word document into 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 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 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 Aspose.Words Cloud SDK for Dart to render a Word document to PDF format 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 processings. So a 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.

Besides the code snippet, Aspose.Words Cloud API Reference is also the easiest way to try out our APIs right away in your browser! It allows you to effortlessly interact and try out every single operation our APIs have exposed. 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