← All postsEngineering

Convert or Merge DOCX to PDF in Your App

Part of Hosting by framework

Quotes, contracts, invoices and reports usually start as a Word or Excel file, and the customer expects a PDF. Sometimes it's several files that belong together: a contract, its schedule and a signed scan. Your app needs to do three things: convert, merge, and make the scanned pages searchable.

Try it

Add a few files, drag them into order, tick “Make searchable” if one of them is a scan, and you get one PDF back. Nothing is kept.

PDF, Word, Excel, PowerPoint, PNG or JPEG · 5 files, 20 MB in all · OCR reads the first 5 pages of each · 5 runs an hour

How it works

  1. Convert. Word, Excel, PowerPoint and OpenDocument files go to LibreOffice, which lays them out the way Word does and prints a PDF.
  2. Make searchable. Scans and photos go through OCR (Tesseract). You get the same page images back with an invisible text layer, so search and copy work.
  3. Merge. pdf-lib copies the pages of each PDF, in the order the user chose, into one document. This part is plain JavaScript and runs in your app.

Every upload is virus-scanned before any of that, since these are files from strangers.

The code

This is the core of the demo above, in Node. render and ocr are the two short helpers from the services docs; each sends a file down a unix socket and reads the PDF back.

import { PDFDocument } from "pdf-lib";
import { render, ocr } from "./services.js"; // the two helpers from canner.ca/docs/services

const isPdf = (buf) => buf.subarray(0, 5).toString() === "%PDF-";

// One file to a PDF: Office files are converted, PDFs are OCR'd when asked.
async function toPdf(file, searchable) {
  if (!isPdf(file)) return (await render(file, "convert")).data;
  if (searchable) return (await ocr(file, "pdf", { lang: "eng+fra" })).data;
  return file;
}

// Several PDFs, in order, to one.
async function merge(pdfs) {
  const out = await PDFDocument.create();
  for (const bytes of pdfs) {
    const doc = await PDFDocument.load(bytes);
    for (const page of await out.copyPages(doc, doc.getPageIndices())) out.addPage(page);
  }
  return out.save();
}

const pdfs = [];
for (const file of uploads) pdfs.push(await toPdf(file, searchable)); // one at a time, in order
const merged = await merge(pdfs);

The demo adds a bot check, a size limit and an hourly limit per visitor, kept in the project's cache. The whole server is one file of about 260 lines. Send one file at a time: a project runs one conversion or OCR job at once.

Why not install LibreOffice yourself?

You can. It's a large install, it needs its own profile folder, and the first conversion after a cold start is slow. Tesseract adds its own packages and language data. Our DOCX to PDF guide and searchable PDF guide cover doing it yourself, and the errors you'll meet.

Build your own

On Canner, turn on Document rendering (PDF) and Text recognition (OCR) in your project's Services tab and redeploy. Both come with the Live plan, run on the same servers as your app in Canada, and keep nothing.

About the author

Colin Shand is the founder of Canner, a Canadian deployment platform operated from Quebec. He writes about sovereign infrastructure, the Canadian startup ecosystem, and building independently.

Try Canner.

Drop a project, get a live URL on Canadian infrastructure in about 30 seconds. Free tier available.