Word to PDF Aspose provides a high fidelity API to programmatically convert Word documents to PDF format and in the opposite directions with professional quality. The combined use of Adobe and Microsoft Office technologies has a lot to offer to the end-user.
These major document formats, including DOCX , DOC , RTF , ODT and PDF , 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.
Despite similarities, Word and PDF documents have considerable differences in the operational capabilities.
Word document formats are great for collaborative development, but they aren’t always the best choice for distributing, as they can be easily modified without author’s permission. PDF documents, in contrast, support multilevel security options and are difficult to extract information. The overall Adobe’s technology makes the forgery of PDF documents a complex task.
You may require to convert a editable DOCX or DOC to an immutable PDF in order to protect document against undesirable modifications.
Usage examples with cURL and Postman
You can carry out REST API interactions using the REST Service Endpoint defines a publicly accessible programming interface and lets you run Word to PDF conversions conversions directly from a web browser.
You can use cURL command-line tool to access Aspose.Words web services and convert Word documents to PDF format easily. The following code demonstrates how to convert DOCX to PDF with cURL. Download sample sample.docx and output sample.pdf files for testing purposes.
cURL Request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
curl -v "https://api.aspose.cloud/v4.0/words/convert?format=pdf" \
-X PUT \
-H "Content-Type: multipart/form-data" \
-H "Authorization: Bearer ####################" \
-F Document="@Sample.docx"
To get a JWT token use this instruction
Postman Request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{
"info": {
"name": "ConvertDocument_2",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"auth": {
"type": "oauth2",
"oauth2": [
{
"key": "clientSecret",
"value": "{{CLIENT_SECRET}}",
"type": "string"
},
{
"key": "clientId",
"value": "{{CLIENT_ID}}",
"type": "string"
},
{
"key": "addTokenTo",
"value": "header",
"type": "string"
},
{
"key": "client_authentication",
"value": "body",
"type": "string"
},
{
"key": "accessTokenUrl",
"value": "https://api.aspose.cloud/connect/token",
"type": "string"
},
{
"key": "grant_type",
"value": "client_credentials",
"type": "string"
},
{
"key": "tokenName",
"value": "Access Token",
"type": "string"
}
]
},
"item": [
{
"name": "convertRequest",
"protocolProfileBehavior": {
"disableBodyPruning": true
},
"request": {
"method": "PUT",
"header": [
],
"body": {
"mode": "formdata",
"formdata": [
{
"key": "Document",
"type": "file",
"src": "Sample.docx"
}
]
},
"url": {
"raw": "https://api.aspose.cloud/v4.0/words/convert?format=pdf",
"protocol": "https",
"host": [
"api",
"aspose",
"cloud"
],
"path": [
"v4.0",
"words",
"convert"
],
"query": [
{
"key": "Format",
"value": "pdf",
"description": ""
}
]
}
},
"response": []
}
],
"variable": [
]
}
To get a JWT token use this instruction
Server Response
Copy {
"SaveResult" : {
"SourceDocument" : {
"Href" : "https://api.aspose.cloud/v4.0/words/sample.docx" ,
"Rel" : "self" ,
"Type" : null ,
"Title" : null
},
"DestDocument" : {
"Href" : "sample.pdf" ,
"Rel" : "saved" ,
"Type" : null ,
"Title" : null
},
"AdditionalItems" : [
]
},
"Code" : 200 ,
"Status" : "OK"
}
Aspose.Words Cloud SDK Family
Using SDK is the best way to speed up the development. Our Cloud SDKs take care of low-level details and let you focus on your primary tasks.
Please check out the GitHub repository for a complete list of Aspose.Words SDKs.
The following code examples demonstrate how to convert Word to PDF programmatically using various Aspose.Words SDKs:
Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os
import asposewordscloud
import asposewordscloud.models.requests
from asposewordscloud.rest import ApiException
from shutil import copyfile
words_api = WordsApi(client_id = '####-####-####-####-####', client_secret = '##################')
request_document = open('Sample.docx', 'rb')
convert_request = asposewordscloud.models.requests.ConvertDocumentRequest(document=request_document, format='pdf')
words_api.convert_document(convert_request)
Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.aspose.words.cloud.*;
import com.aspose.words.cloud.api.*;
import com.aspose.words.cloud.model.*;
import com.aspose.words.cloud.model.requests.*;
import com.aspose.words.cloud.model.responses.*;
import java.nio.file.Files;
import java.nio.file.Paths;
ApiClient apiClient = new ApiClient(/*clientId*/ "####-####-####-####-####", /*clientSecret*/ "##################", null);
WordsApi wordsApi = new WordsApi(apiClient);
byte[] requestDocument = Files.readAllBytes(Paths.get("Sample.docx").toAbsolutePath());
ConvertDocumentRequest convertRequest = new ConvertDocumentRequest(requestDocument, "pdf", null, null, null, null, null, null, null);
wordsApi.convertDocument(convertRequest);
Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as fs from "fs";
const clientId = "####-####-####-####-####";
const secret = "##################";
const wordsApi = new WordsApi(clientId, secret);
const requestDocument = fs.createReadStream("Sample.docx");
const convertRequest = new model.ConvertDocumentRequest({
document: requestDocument,
format: "pdf"
});
wordsApi.convertDocument(convertRequest)
.then((convertRequestResult) => {
// tslint:disable-next-line:no-console
console.log("Result of convertRequest: ", convertRequestResult);
});
C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Aspose.Words.Cloud.Sdk;
using Aspose.Words.Cloud.Sdk.Model;
using Aspose.Words.Cloud.Sdk.Model.Requests;
var config = new Configuration { ClientId = "####-####-####-####-####", ClientSecret = "##################" };
var wordsApi = new WordsApi(config);
using var requestDocument = File.OpenRead("Sample.docx");
var convertRequest = new ConvertDocumentRequest(requestDocument, "pdf");
await wordsApi.ConvertDocument(convertRequest);
PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php
use Aspose\Words\WordsApi;
use Aspose\Words\Model\Requests\{ConvertDocumentRequest};
$clientId = '####-####-####-####-####';
$secret = '##################';
$wordsApi = new WordsApi($clientId, $secret);
$requestDocument = "Sample.docx";
$convertRequest = new ConvertDocumentRequest(
$requestDocument, "pdf", NULL, NULL, NULL, NULL, NULL, NULL, NULL);
$wordsApi->convertDocument($convertRequest);
C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "aspose_words_cloud.h"
using namespace aspose::words::cloud;
auto config = std::make_shared<ApiConfiguration>(/*clientId*/ L"####-####-####-####-####", /*clientSecret*/ L"##################");
auto wordsApi = std::make_shared<WordsApi>(config);
auto requestDocument = std::shared_ptr<std::istream>(new std::ifstream(std::filesystem::path(L"Sample.docx"), std::istream::binary));
std::shared_ptr<requests::ConvertDocumentRequest> convertRequest(
new requests::ConvertDocumentRequest(
requestDocument, std::make_shared<std::wstring>(L"pdf")));
wordsApi->convertDocument(convertRequest);
Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{
"ClientId": "####-####-####-####-####",
"ClientSecret": "##################",
"BaseUrl": "https://api.aspose.cloud"
}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import (
"os"
"github.com/aspose-words-cloud/aspose-words-cloud-go/dev/api/models")
config, _ := models.NewConfiguration("config.json")
wordsApi, ctx, _ := api.CreateWordsApi(config)
requestDocument, _ := os.Open("Sample.docx")
convertRequestOptions := map[string]interface{}{}
convertRequest := &models.ConvertDocumentRequest{
Document: requestDocument,
Format: ToStringPointer("pdf"),
Optionals: convertRequestOptions,
}
_, _ = wordsApi.ConvertDocument(ctx, convertRequest)
Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'aspose_words_cloud'
AsposeWordsCloud.configure do |config|
config.client_data['ClientId'] = '####-####-####-####-####'
config.client_data['ClientSecret'] = '##################'
end
@words_api = WordsAPI.new
request_document = File.open('Sample.docx')
convert_request = ConvertDocumentRequest.new(document: request_document, format: 'pdf')
@words_api.convert_document(convert_request)
Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import AsposeWordsCloud
let config = Configuration(clientId: "####-####-####-####-####", clientSecret: "##################");
let api = try WordsAPI(configuration: config);
let requestDocument = InputStream(url: URL(string: "Sample.docx"))!;
let convertRequest = ConvertDocumentRequest(document: requestDocument, format: "pdf");
_ = try api.convertDocument(request: convertRequest);
Dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:io' ;
import 'package:aspose_words_cloud/aspose_words_cloud.dart' ;
final clientId = "####-####-####-####-####" ;
final clientSecret = "##################" ;
final config = Configuration (clientId, clientSecret);
final wordsApi = WordsApi (config);
final requestDocument = (await File ('Sample.docx' ).readAsBytes ()).buffer.asByteData ();
final convertRequest = ConvertDocumentRequest (requestDocument, 'pdf' );
await wordsApi.convertDocument (convertRequest);
See also