Unlock the Power of Direct PDF Editing with WebViewer 10.7

How to Convert Office Documents to PDF in the Browser Without Installing Office

By Apryse | 2023 Aug 17

Sanity Image
Read time

4 min

In the modern business landscape, sharing and archiving documents in a reliable and universally accessible format is paramount. 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 Apryse PDF 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 this blog post, we'll explore how to convert Office documents to PDF using the Apryse SDK and JavaScript, running entirely within the browser.

But surely that’s easy, you can just use Export from Word.

Copied to clipboard

If you have Office installed, then creating a PDF is simple.

The awesome thing about this solution is that there is no need for Office to be installed, either on your machine or on a server somewhere.

That’s right. The conversion occurs entirely without the need for Office.

Why would you want to convert in the browser?

Copied to clipboard

When the conversion is performed within the browser it can start immediately without the delay associated with it being uploaded to a server. Furthermore, from a security point of view, a browser-based conversion means that the document never leaves your machine. This entirely removes the need for it to be transferred over the internet and stored, even briefly, on someone else’s server, with the associated risks of user data being stolen or shared.

From the website owner’s point of view, conversions don’t require high-powered (and expensive) servers, and the risk of malicious users uploading dangerous files onto the server is entirely avoided.

Having the conversion occur within the browser does require the conversion code to be transferred to the browser. This is only about 11MB, and after the first load, it will be fetched from the local cache rather than over the network.

Apryse SDK Trial Key

Copied to clipboard

If you don't already have an Apryse account, go to https://dev.apryse.com and register a new account with Apryse. This allows Apryse to grant you a demo license key which will be used with the Apryse SDK to enable demo functionality.

Blog image

Figure 1- The Developer Portal

Log into https://dev.apryse.com with your registered account. For this guide, we’ll be developing a JavaScript Web application, so select Web platform.

Click on the reveal button to get your personalized trial key.

Blog image

Figure 2 - Download Center Platform and Trial Key

Sample Project for Converting a Document to PDF

Copied to clipboard

This sample uses version 10.3 of the Apryse WebViewer SDK and the trial license key which you have just revealed.

The sample is intended to show how a single document 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 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.

Setting Up Your Project: 

  • Create a new directory for your project. I used the name myProject.
  • Get the WebViewer code, either by running npmi @pdftron/webviewer or, as I did, by downloading it from here and extracting the zip file to a folder.
  • Copy the core folder from node_modules/@pdftron/webviewer/public/coreto a location that can serve static assets (if you downloaded the SDK, you will find the core folder in WebViewer/lib/).
Blog image
  • Create a new text file in the folder called index.html.
Blog image
  • Copy the following code into the index.html file.
<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
    
    // make sure that you use a real license key, rather than just using the 
    // text above, otherwise you will get an error when the conversion occurs 
    await Core.PDFNet.initialize(); 
    
     // perform the conversion with no optional parameters 
     const buf = await Core.officeToPDFBuffer('Fishermen.docx', { l: licenseKey }); 
     const blob = new Blob([buf], { type: 'application/pdf' }); 
     
     // optionally save the blob to a file or upload to a server 
     // in this example the blob will be displayed within the browser 
     var link=window.URL.createObjectURL(blob); 
     window.location=link; 
   })() 
  </script> 
  </body> 
</html> 
  • Depending on where you placed files, you may need to update the path to the Apryse library.
  • In this example, I am converting the file ‘Fisherman.docx’, which is located in the same folder as the index.html file.
  • This page is now ready to use. I used a simple http-server running from PowerShell for this example, but you can use whatever web server you prefer.
Blog image

Figure 3 - output from the httpserver

As soon as the page loads, the Fisherman.docx file is converted into a PDF, and the resulting document is displayed within the browser, from where the user can view or save it. 

Blog image

Figure 4 - An example Word document, now converted to a PDF

In production, of course, you might prefer to save the PDF directly to a file or upload it to a server using whatever mechanism that you write.

Word. Is that all?

Copied to clipboard

Absolutely not. In addition to converting from Word, the SDK can convert from Excel and PowerPoint, including using the legacy Office formats of .doc, .xls, and .ppt.

Excel spreadsheets can be converted into a multiple-page PDF with each page laid out in the same way as the original spreadsheet.

Blog image

Figure 5 - An example Excel spreadsheet now converted to a PDF (source: https://create.microsoft.com/en-us/template/small-business-cash-flow-forecast-f7200e29-427a-4653-afde-52f846e590a1)

And PowerPoint presentations can also be converted into multi-page PDFs.

Blog image

Figure 6 - A PowerPoint presentation now converted to a PDF

It is also possible to convert .txt, .odt, .rtf and .doc files to PDF in the same way.

Conclusion

Copied to clipboard

Apryse offers a simple mechanism for converting Office documents to PDF without Office being installed. 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.

Apryse's JavaScript library provides developers with an easy-to-use API that enables seamless integration into web applications. This library enables you to not only convert Office documents to PDF but also perform a variety of PDF-related operations, such as editing, annotating, and extracting content.

Apryse is a leading provider of cross-platform PDF development tools. With its comprehensive PDF processing capabilities, it offers an excellent solution for converting Office documents to PDF.

You can check out our interactive demo for a demonstration of how document conversions work in the ApryseWebViewer, or see the documentation for the Web SDK to get started quickly. Don’t forget, you can also reach out to us on Discord if you have any issues.

Sanity Image

Apryse

Share this post

email
linkedIn
twitter