Available Now: Explore our latest release with enhanced accessibility and powerful IDP features
By Apryse | 2023 Aug 17
8 min
Tags
office conversion
javascript
docx to pdf
In the modern business landscape, sharing and archiving documents in a reliable and universally accessible format is paramount. Portable Document Format (PDF) stands out as a versatile and widely supported format that ensures consistent presentation across different platforms. Converting Office documents such as Word, Excel, and PowerPoint files into PDFs is a common requirement.
The AprysePDF SDK can be used with multiple languages and frameworks to convert Office Documents to PDF, with both client-side (within the browser) and server-side conversions.
In a previous blog I wrote about how to convert Office documents to PDF using JavaScript running within the browser. In this post, we'll explore how to convert Office documents using the ApryseSDK JavaScript running on a server.
The Apryse JavaScript library can be downloaded using Node, as can a package that contains dozens of samples. In this blog we will walk through getting started with the sample for converting a Word document to PDF, then extend it to see some of the other functionality that is available.
If you have Office installed, then creating a PDFusing that on your own machine is simple.
But what do you do if you don’t have Office installed?
Theoretically a server could be set up that uses Office to perform the conversions within the Web server backend, but there are several issues with that:
The awesome thing about the Apryse solution is that there is no need for Office to be installed, either on the user’s machine, or on a server, for the conversion to occur.
That’s right. The conversion occurs entirely without the need for Office.
While there are some advantages of performing the conversion in a browser, conversion on a server is likely to be faster for complex large files and is likely to result in the same conversion every time. Converting a file relies, at least in part, on locally available fonts, so a browser-based conversion performed on one machine could potentially look different from the conversion of the same file that was performed on a different machine which had different fonts available.
Apryse offers solutions for both server-side and within-browser conversions, but in this example, we will just look at the server-side option.
This blog was written using version 10.3.0 of the Apryse SDK and a Trial license key.
The sample project is intended to show how a singledocument, in a hard coded location, can be converted to PDF.
In reality you are likely to want to be able to specify which document is to be converted, and what you want to do with the PDF once the conversion is complete. As such the code should be considered to be an example of how to convert a file and see the result, rather than as a template of how to write an entire document processing solution.
If you don't already have an Apryse account, go to https://dev.apryse.com and register a new account. This allows Apryse to grant you a demo license key which will be used with the Apryse SDK to enable demo functionality.
Figure 1 - The Developer Portal
Log into https://dev.apryse.com with your registered account. The JavaScript SDK is available for Windows, Linux and macOS, so select the platform that you are using.
Click on the reveal button to get your personalized Trial key.
Figure 2 - Download Center Platform and Trial Key
There is a great guide to getting started available from https://docs.apryse.com/try-now/ which will lead you through the steps to creating an application on Windows, macOS or Linux.
Figure 3 - the Guide for Getting Started
However, for this example I will useVSCodefor development and Node to install the Apryse SDK and samples.
You will need VSCode, Node (I used Node 16.6.0), the trial license key that you downloaded from the Apryse website
This will copy files onto your machine that include the sample code, and the Node modules that are required to run the samples.
// first the one-line conversion function
awaitsimpleDocxConvert('Fishermen.docx', 'Fishermen.pdf');
// then the more flexible line-by-line conversion API
awaitflexibleDocxConvert('the_rime_of_the_ancient_mariner.docx',
'the_rime_of_the_ancient_mariner.pdf');
// conversion of RTL content
awaitflexibleDocxConvert('factsheet_Arabic.docx', 'factsheet_Arabic.pdf');
This code specifies the files that should be converted from Office to PDF as hardcoded locations within the samples package that we have downloaded.
Before we go any further, we need to update the license file with the trial license key
Find the fileLicenseKey.js
And paste in your license key.
Having done that ,let’s look at the files that we are going to convert. This is just one of them, but feel free to look at them all.
Figure 4 - the first page of one of the Word Documents that will be Converted by the Sample Code.
When you are ready you can run the conversion code by entering Node OfficeToPDFTest.js in a terminal window.
After a few moments the terminal will show the results of the function.
You can see that there are three files there that have recently been created.
Opening each in turn will show that we have created PDFs from the original documents.
Figure 5 - A PDF that was Created from a Simple Word document
Figure 6 - Another PDF that was Created from a Word document
Figure 7 - A PDF that was Created from an Arabic Language Word document, Illustrating Right-to-Left Text.
You could now use this code as the back-end to a website with the Word document being uploaded, and the converted PDF being returned.
There are two different functions in the sample.
simpleDocxConvert and flexibleDocxConvert
In this blog I will just look at the simpler method.
constsimpleDocxConvert=async (inputFilename, outputFilename) => {
// perform the conversion with no optional parameters
constpdfdoc=awaitPDFNet.Convert.officeToPdfWithPath(inputPath+inputFilename);
// save the result
awaitpdfdoc.save(outputPath+outputFilename,
PDFNet.SDFDoc.SaveOptions.e_linearized);
// And we're done!
console.log('Saved '+outputFilename);
}
One of those lines is for logging, so at its very simplest all that needs to be done to convert an Office document to a PDF is to create a PdfDocument, then to save that PdfDocument.
That is extremely simple.
You can see that you convert a Word document to a PDF in just two lines of code.
Talk about simply getting a great result!
I will write more about the function flexibleDocxConvert in a later blog, but, for now, it is enough to say that it illustrates how the code can be used with various options, or within a multithreaded environment to monitor and cancel conversions. I will describe that further in a separate blog post.
Absolutely.
In addition to converting from Word, the SDK can convert from Excel and PowerPoint to PDF, and even supports legacy Office formats: .doc, .xls and .ppt.
At its simplest, all that needs to be done to convert from other Office document types is to include the file extension when passing the file to the conversion method.
The function simpleDocxConvert isn’t a great name, as it suggests that the Apryse SDK is less powerful than it actually is, but that is part of the sample code, not part of the SDK, so feel free to change it if you want.
For example,an Excel Spreadsheetcan be converted into a multiple page PDF with each page laid out in the same way as the original spreadsheetby copying the file to the TestFiles folder then using:
awaitsimpleDocxConvert('Cashflow.xlsx', 'Cashflow.pdf');
The SDK is clever enough to know that .xlsx means that a conversion from Excel to PDF is required.
Figure 8 - An example Excel spreadsheet Now Converted to a PDF
In the same way, PowerPoint presentations can be convertedinto multi-page PDFs using:
awaitsimpleDocxConvert('WW1Cryptography.pptx', 'WW1Cryptography.pdf');
Figure 9: A PowerPoint Presentation Now Converted to a PDF
Apryse offers a simple mechanism for converting Office documents to PDF without the need for Office to be installed. This can be done with just a few lines of code that uses default options. More complex options exist to allow the conversion mechanism to be tailored to your requirements.
These powerful conversion capabilities, coupled with the ease of integration provided by its JavaScript library, make it the best choice for developers aiming to enhance their document processing workflows. Whether you're building a document management system, an online collaboration platform, or any other application involving Office documents, Apryse can help you provide a seamless and efficient conversion process.
With Apryse's capabilities at your disposal, you can enhance your application's functionality and provide users with a reliable way to convert and work with Office documents in PDF format.
In addition to converting Office documents to PDF, Apryse offersmany tools for editing and handling both Office Documents and PDFs, including converting PDFs into Office documents.
See the code in action, using the SDK for creating PDFs from Word documents, Excel spreadsheets and PowerPoint presentations. When you are ready to get started,see the documentation for the SDK to get started quickly. Don’t forget, you can also reach out to us on Discord if you have any issues.
Tags
office conversion
javascript
docx to pdf
Apryse
Share this post
PRODUCTS
Enterprise
Small Business
Popular Content