Cannot convert encrypted XOD to PDF using server SDK

Product: Node.JS SDK
Product Version: 9.4.0

I’m trying to convert an aes encrypted XOD file to PDF using the Node.JS SDK. I’m trying to use the initStdSecurityHandlerUString function for this, but it doesn’t appear to be working - I suspect this function is only intended for “standard” pdf encryption, not the xod encryption. How can I accomplish this?

Code

"use strict";

const {
	PDFNet
} = require("@pdftron/pdfnet-node");
const LICENSE_KEY = "";

PDFNet.runWithCleanup(async () => {
		let in_file = "encrypted-foobar12.xod"; // https://pdftron.s3.amazonaws.com/downloads/pl/encrypted-foobar12.xod
		let out_file = "decrypted-foobar12.pdf";
		try {
			const pdfdoc = await PDFNet.PDFDoc.create();
			await pdfdoc.initStdSecurityHandlerUString("foobar12");
			await PDFNet.Convert.toPdf(pdfdoc, in_file);
			await pdfdoc.save(out_file, PDFNet.SDFDoc.SaveOptions.e_linearized);
			console.log("Converted file: " + in_file + "\nto: " + out_file);
		} catch (err) {
			console.log("Unable to convert file " + in_file);
			console.log(err);
		}
	}, LICENSE_KEY)
	.catch(function(error) {
		console.log("Error: " + JSON.stringify(error));
	})
	.then(function() {
		return PDFNet.shutdown();
	});

Output

Unable to convert file encrypted-foobar12.xod
{
  message: 'Exception: \n' +
    '\t Message: An error occurred while converting the file.\n' +
    'Detailed error:\n' +
    '\tException: \n' +
    '\t Message: XML Parsing error\n' +
    '\t Conditional expression: false\n' +
    '\t Version      : 9.4.0-29d3f4d\n' +
    '\t Platform     : Linux\n' +
    '\t Architecture : AMD64\n' +
    '\t Filename     : XMLParser.cpp\n' +
    '\t Function     : Advance\n' +
    '\t Linenumber   : 141\n' +
    '\n' +
    '\t Conditional expression: false\n' +
    '\t Version      : 9.4.0-29d3f4d\n' +
    '\t Platform     : Linux\n' +
    '\t Architecture : AMD64\n' +
    '\t Filename     : Convert.cpp\n' +
    '\t Function     : ToPdf\n' +
    '\t Linenumber   : 2033\n',
  type: 'PDFWorkerError'
}

Currently it is not possible to convert an encrypted XOD file to PDF.

Could you elaborate on what happened to the original/source file that the XOD represents?

Our use case for this would be freeing up space on our server by only storing the encrypted .xod files, then converting them back to PDFs on the fly - that way we can keep original files separate and don’t have to store what is essentially two copies of each document. We want to keep them stored as encrypted .xod files so that users can’t easily download them, but still allow downloads under certain circumstances.

The conversion to XOD is often lossy in some way, so going FileTypeX → XOD → FileTypeX will not give you the identical file.

You should always keep the original file.

I would suggest one of the following, if storage space is an issue for you.

  1. Switch to streaming XOD conversion
    PDFTron Systems Inc. | Documentation

  2. Stop doing XOD conversion, and instead connect WebViewer Server to WebViewer.
    PDFTron Systems Inc. | Documentation

  3. Switch to PDF viewing in WebViewer.

Options (2) and (3) might require you to purchase additional licensing, your PDFTron account manager could help you here.

This guide is helpful in picking the options above.