NOW AVAILABLE: Summer 2025 Release
By Garry Klooesterman | 2025 Jul 24
4 min
Tags
docx to pdf
javascript
Summary: Working with Office file formats such as DOCX can be tricky without the right tools. This blog discusses how to programmatically convert DOCX to PDF using JavaScript and Apryse Web SDK and without external dependencies, allowing users to easily view and share information.
Office file formats, particularly DOCX, are some of the most commonly used file formats on the planet. DOCX is a versatile file format for many types of documents including:
Viewing DOCX files, let alone editing them, can be difficult though if you don’t have the right tools. For example, see our video: Why do Microsoft Word documents look different on different computers?
This is where converting to PDF is ideal as they can be easily and reliably viewed across many devices and systems.
Apryse Web SDK allows you to directly convert Office formats such as DOCX, XLSX, and PPTX to PDF, and back for that matter, on any platform or browser. This is done securely, without external dependencies, including Office or LibreOffice.
With our DOCX Editor add-on for WebViewer, users can edit PDF content, tables, text, or images as Word files.
In this guide, we’ll cover how to:
Everything is done in client-side JavaScript and can be performed in a vanilla JS app, React, or any other framework of your choice.
Note: This project can also be done server-side. See our Office to PDF using Node.js guide for more details.
Developers can create apps that can automatically convert DOCX to PDF using JavaScript, allowing users to work with a more flexible PDF format for use cases such as:
Easy Sharing: PDFs are easily viewed on any device or platform making it an ideal format for sharing information.
Document Preservation and Archiving: Files can be converted to PDF/A format for long-term storage. This format ensures that all elements needed to display the file reliably are contained in the one file.
Sensitive Data Protection: PDF format offers encryption, digital signatures, redaction, and password protection for viewing, copying, and printing. This makes PDFs ideal for sharing confidential documents.
Converting a DOCX to PDF right in your browser is pretty straightforward with Apryse Web SDK. Let’s get started!
1. Install WebViewer using the following code:
npm i @pdftron/webviewer
Note: You can also download it here.
2. Copy the core folder from the node_modules/@pdftron/webviewer/public/core to a location that can serve static assets. For example, the public folder in a React app.
Note: If you downloaded the SDK, you can find the core folder in WebViewer/lib/. You can also check out our documentation for more details on copying WebViewer static assets.
3. Reference webviewer-core.min.js script in the index.html file and create a JS function to perform the conversion.
<html>
<body>
<script src="../core/webviewer-core.min.js"></script>
<script>
(async function() {
Core.setWorkerPath('../core');
const licenseKey = 'Insert commercial license key here after purchase';
await Core.PDFNet.initialize();
// perform the conversion with no optional parameters
const buf = await Core.officeToPDFBuffer('/path/to/file.docx', { l: licenseKey });
//optionally save the blob to a file or upload to a server
const blob = new Blob([buf], { type: 'application/pdf' });
})()
</script>
</body>
</html>
That’s it! When the script above runs, it automatically converts the DOCX file to PDF.
WebViewer will automatically use substitute fonts hosted on a web server when a font is not embedded in the document. The fallback fonts are hosted by Apryse by default, but if this is a concern, you can host the fonts on your own servers. You can also customize the font package to better suit your needs.
You can download the self serve font package to get started.
To serve the default font package from your own server:
1. Expand the webfont archive into a location that is accessible by a URL.
2. Set the custom font URL in your WebViewer config file using the following code:
Core.setCustomFontURL("https://www.somealternateurl.com/chosensubpath/");
Note: Don’t forget to replace the URL with your own.
WebViewer downloads the fonts.json file, which lists the available fonts and matching rules, and then selects the appropriate fonts to download as needed.
Note: You can also customize the font set, but that’s beyond what we’re doing in this guide.
We hope you found this article helpful! As you can see, it’s quite easy to set up a programmatic DOCX to PDF converter with Apryse Web SDK. Whether your users are converting invoices or monthly reports, you’ll be able to create an app that delivers the automation they need to streamline their workflows.
To learn more about PDF generation, check out our complete Apryse PDF SDK.
See how this works in our live demo! You can also get started now, contact our sales team for any questions, or join our Discord community for support and discussions.
Tags
docx to pdf
javascript
Garry Klooesterman
Senior Technical Content Creator
Share this post
PRODUCTS
Platform Integrations
End User Applications
Popular Content