DOCX, XSLX to PDF Conversion - OfficeToPDF

Sample code for using Apryse Server SDK to convert Office documents to PDF (including Word, Excel, PowerPoint and Publisher) without needing any external dependencies or MS Office licenses. Office to PDF conversion can be performed on a Linux or Windows server to automate Office-centric workflows, or entirely in the user's client (web browser, mobile device). The conversion functionality can be combined with our Viewer to display or annotate Office files (docx, xlsx, pptx) on all major platforms, including Web, Android, iOS, Xamarin, UWP, and Windows. Samples provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby, Go and VB.

Learn more about our Server SDK and Office Document Conversion Library.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6//------------------------------------------------------------------------------
7// The following sample illustrates how to use the PDF::Convert utility class
8// to convert MS Office files to PDF
9//
10// This conversion is performed entirely within the PDFNet and has *no*
11// external or system dependencies dependencies -- Conversion results will be
12// the same whether on Windows, Linux or Android.
13//
14// Please contact us if you have any questions.
15//------------------------------------------------------------------------------
16
17const { PDFNet } = require('@pdftron/pdfnet-node');
18const PDFTronLicense = require('../LicenseKey/LicenseKey');
19
20((exports) => {
21 'use strict';
22
23 exports.runOfficeToPDF = () => {
24
25 const inputPath = '../TestFiles/';
26 const outputPath = inputPath + 'Output/';
27
28 const simpleDocxConvert = async (inputFilename, outputFilename) => {
29 // perform the conversion with no optional parameters
30 const pdfdoc = await PDFNet.Convert.officeToPdfWithPath(inputPath + inputFilename);
31
32 // save the result
33 await pdfdoc.save(outputPath + outputFilename, PDFNet.SDFDoc.SaveOptions.e_linearized);
34
35 // And we're done!
36 console.log('Saved ' + outputFilename);
37 }
38
39 const flexibleDocxConvert = async (inputFilename, outputFilename) => {
40 // Start with a PDFDoc (the conversion destination)
41 const pdfdoc = await PDFNet.PDFDoc.create();
42 pdfdoc.initSecurityHandler();
43
44 const options = new PDFNet.Convert.OfficeToPDFOptions();
45
46 // set up smart font substitutions to improve conversion results
47 // in situations where the original fonts are not available
48 options.setSmartSubstitutionPluginPath(inputPath);
49
50 // create a conversion object -- this sets things up but does not yet
51 // perform any conversion logic.
52 // in a multithreaded environment, this object can be used to monitor
53 // the conversion progress and potentially cancel it as well
54 const conversion = await PDFNet.Convert.streamingPdfConversionWithPdfAndPath(
55 pdfdoc, inputPath + inputFilename, options);
56
57 // Print the progress of the conversion.
58 /*
59 console.log('Status: ' + await conversion.getProgress() * 100 + '%, '
60 + await conversion.getProgressLabel());
61 */
62
63 // actually perform the conversion
64 // this particular method will not throw on conversion failure, but will
65 // return an error status instead
66
67 while (await conversion.getConversionStatus() === PDFNet.DocumentConversion.Result.e_Incomplete) {
68 await conversion.convertNextPage();
69 // print out the progress status as we go
70 /*
71 console.log('Status: ' + await conversion.getProgress() * 100 + '%, '
72 + await conversion.getProgressLabel());
73 */
74 }
75
76 if (await conversion.getConversionStatus() === PDFNet.DocumentConversion.Result.e_Success) {
77 const num_warnings = await conversion.getNumWarnings();
78
79 // print information about the conversion
80 for (let i = 0; i < num_warnings; ++i) {
81 console.log('Conversion Warning: ' + await conversion.getWarningString(i));
82 }
83
84 // save the result
85 await pdfdoc.save(outputPath + outputFilename, PDFNet.SDFDoc.SaveOptions.e_linearized);
86 // done
87 console.log('Saved ' + outputFilename);
88 }
89 else {
90 console.log('Encountered an error during conversion: '
91 + await conversion.getErrorString());
92 }
93 }
94
95
96 const main = async () => {
97
98 PDFNet.addResourceSearchPath('../Resources');
99
100 try {
101 // first the one-line conversion function
102 await simpleDocxConvert('Fishermen.docx', 'Fishermen.pdf');
103
104 // then the more flexible line-by-line conversion API
105 await flexibleDocxConvert('the_rime_of_the_ancient_mariner.docx',
106 'the_rime_of_the_ancient_mariner.pdf');
107
108 // conversion of RTL content
109 await flexibleDocxConvert('factsheet_Arabic.docx', 'factsheet_Arabic.pdf');
110 } catch (err) {
111 console.log(err);
112 }
113
114 console.log('Done.');
115 };
116
117 PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function (error) {
118 console.log('Error: ' + JSON.stringify(error));
119 }).then(function () { return PDFNet.shutdown(); });
120
121 };
122 exports.runOfficeToPDF();
123})(exports);
124// eslint-disable-next-line spaced-comment
125//# sourceURL=OfficeToPDFTest.js

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales