How to Convert Word (DOC/DOCX) to PDF in React
In this blog post, you’ll learn how to convert Word (DOC and DOCX) to PDF using React and PSPDFKit. Both DOC and DOCX files are converted to PDF in the client and don’t require any server-side processing.
Client-Side Word-to-PDF Conversion
To convert Office documents such as DOC or DOCX files to PDF, PSPDFKit for Web relies entirely on its own technology built from the ground up, and it doesn’t depend on third-party tools such as Microsoft Office. With PSPDFKit for Web, Word documents can be converted into PDF headlessly without a visual interface, or automatically as the document is loaded in the viewer.
-
Easily integrate client-side Word-to-PDF conversion in your app or workflow.
-
Open Word files in the viewer or convert directly in your workflow.
-
No MS interop or license required.
-
Doesn’t rely on third-party tools like LibreOffice.
-
We work directly with you to refine and improve conversion results.
Unlocking More Capabilities with Our Word Viewer
By combining client-side Word-to-PDF conversion with our standalone viewer, you unlock all the document processing capabilities available with PSPDFKit for Web. Users can now view, collaborate on, edit, and sign Office files directly in the web browser.
-
Text editing — Edit text directly in the displayed Word document.
-
Page manipulation — Organize documents by adding, removing, or rearranging pages.
-
Annotations — Boost collaboration by adding text highlights, comments, or stamps.
-
Adding signatures — Draw, type, or upload a signature directly to a Word document.
Requirements to Get Started
To get started, you’ll need:
-
A package manager compatible with npm. This guide contains usage examples for Yarn and the npm client (installed with Node.js by default).
Creating a New React Project
-
Create a new React app using the Create React App tool:
yarn create react-app pspdfkit-react-example
npx create-react-app pspdfkit-react-example
-
Change to the created project directory:
cd pspdfkit-react-example
Adding PSPDFKit to Your Project
-
Add the PSPDFKit dependency:
yarn add pspdfkit
npm install --save pspdfkit
-
Copy the PSPDFKit for Web library assets to the
public
directory:
cp -R ./node_modules/pspdfkit/dist/pspdfkit-lib public/pspdfkit-lib
The code above will copy the pspdfkit-lib
directory from within node_modules/
into the public/
directory to make it available to the SDK at runtime.
-
Make sure your
public
directory contains apspdfkit-lib
directory with the PSPDFKit library assets.
Displaying a PDF
-
Add a Word (DOC, DOCX) document you want to display to the public directory. You can use our demo document as an example.
-
Add a component wrapper for the PSPDFKit library and save it as
components/PdfViewerComponent.js
:
import { useEffect, useRef } from 'react'; export default function PdfViewerComponent(props) { const containerRef = useRef(null); useEffect(() => { const container = containerRef.current; let instance, PSPDFKit; (async function () { PSPDFKit = await import('pspdfkit'); PSPDFKit.unload(container); instance = await PSPDFKit.load({ // Container where PSPDFKit should be mounted. container, // The document to open. document: props.document, // Use the public directory URL as a base URL. PSPDFKit will download its library assets from here. baseUrl: `${window.location.protocol}//${window.location.host}/${process.env.PUBLIC_URL}`, }); })(); return () => PSPDFKit && PSPDFKit.unload(container); }, []); return ( <div ref={containerRef} style={{ width: '100%', height: '100vh' }} /> ); }
-
Convert the source document to a PDF with the
exportPDF
method:
(async function () {
PSPDFKit = await import('pspdfkit');
PSPDFKit.unload(container);
instance = await PSPDFKit.load({
container,
document: props.document,
baseUrl: `${window.location.protocol}//${window.location.host}/${process.env.PUBLIC_URL}`,
});
+ instance.exportPDF();
})();
-
Optionally, you can use the
outputFormat
flag to generate a PDF/A document. For more details, refer to the guide on converting PDF to PDF/A:
instance.exportPDF({ outputFormat: { conformance: PSPDFKit.Conformance.PDFA_4F, }, });
-
Next, save the resulting document. The
exportPDF
method returns aPromise
that resolves into anArrayBuffer
containing the output PDF document. You can use this array buffer to download the PDF or store it:
instance.exportPDF().then((buffer) => { const blob = new Blob([buffer], { type: 'application/pdf' }); const objectUrl = window.URL.createObjectURL(blob); downloadPdf(objectUrl); window.URL.revokeObjectURL(objectUrl); }); function downloadPdf(blob) { const a = document.createElement('a'); a.href = blob; a.style.display = 'none'; a.download = 'output.pdf'; a.setAttribute('download', 'output.pdf'); document.body.appendChild(a); a.click(); document.body.removeChild(a); }
Here’s the full code for the PdfViewerComponent
:
import { useEffect, useRef } from 'react'; export default function PdfViewerComponent(props) { const containerRef = useRef(null); useEffect(() => { const container = containerRef.current; let instance, PSPDFKit; (async function () { PSPDFKit = await import('pspdfkit'); PSPDFKit.unload(container); instance = await PSPDFKit.load({ // Container where PSPDFKit should be mounted. container, // The document to open. document: props.document, // Use the public directory URL as a base URL. PSPDFKit will download its library assets from here. baseUrl: `${window.location.protocol}//${window.location.host}/${process.env.PUBLIC_URL}`, }); instance.exportPDF().then((buffer) => { const blob = new Blob([buffer], { type: 'application/pdf', }); const objectUrl = window.URL.createObjectURL(blob); downloadPdf(objectUrl); window.URL.revokeObjectURL(objectUrl); }); })(); return () => PSPDFKit && PSPDFKit.unload(container); }, []); return ( <div ref={containerRef} style={{ width: '100%', height: '100vh' }} /> ); } function downloadPdf(blob) { const a = document.createElement('a'); a.href = blob; a.style.display = 'none'; a.download = 'output.pdf'; a.setAttribute('download', 'output.pdf'); document.body.appendChild(a); a.click(); document.body.removeChild(a); }
-
Include the newly created component in
App.js
:
// src/App.js import PdfViewerComponent from './components/PdfViewerComponent'; function App() { return ( <div className="App"> <div className="PDF-viewer"> <PdfViewerComponent document={'document.docx'} /> </div> </div> ); } export default App;
-
Start the app and run it in your default browser:
yarn start
npm start
The output.pdf
file will be downloaded automatically.
Interact with the sandbox by clicking the left rectangle icon and selecting Editor > Show Default Layout. To edit, sign in with GitHub — click the rectangle icon again and choose Sign in. To preview the result, click the rectangle icon once more and choose Editor > Embed Preview. For the full example, click the Open Editor button. Enjoy experimenting with the project!
A Note about Fonts
In client-side web applications for Microsoft Office-to-PDF conversion, PSPDFKit addresses font licensing constraints through font substitutions, typically replacing unavailable fonts with their equivalents — like Arial with Noto. For precise font matching, you can provide your own fonts, embed them into source files, or designate paths to your .ttf
fonts for custom solutions.
Adding Even More Capabilities
Once you’ve deployed your viewer, you can start customizing it to meet your specific requirements or easily add more capabilities. To help you get started, here are some of our most popular React.js guides:
- Adding annotations
- Editing documents
- Filling PDF forms
- Adding signatures to documents
- Real-time collaboration
- Redaction
- UI customization
Conclusion
In this blog post, you learned how to convert Word documents to PDF in React with the PSPDFKit SDK.
If you’re looking for a way to render Office documents in your web application, PSPDFKit offers a React Office viewer. It’s a powerful and flexible library that enables you to open, view, edit, annotate, and sign documents directly in a web browser.
To get started, you can either:
-
Start your free trial to test out the library and see how it works in your application.
-
Launch our demo to see the viewer in action.