Hi there,
I’ve been facing an issue regarding the merge functionality from the Express REST API Utility package (installed through npm) on my server (running in a node js environment):
In the documentation, it is said that we can run this utility client side or server side
It can be used on either the server or the client. (Quotation from the rest api utility documentation page)
, but there is no documentation on how to set it up on a server, so I supposed that the configuration was the same on both sides.
I imported the library using:
const ExpressUtils = require('@pdftron/pdfjs-express-utils').default;
const utils = new ExpressUtils({
serverKey: 'my_server_key',
clientKey: 'my_client_key'
})
Nonetheless, when I launch my server, error instantly happens, saying that window is not defined.
After looking at the package, I can see that normally the library check for isClient || isServer but it doesn’t seem like it’s working.
What can I do ? is there anything wrong with how I instantiated this package ?
NB: I also tried to call directly the endpoint using isomorphic fetch library as you do in the express rest api package, I used ‘cross-blob’ and a nodejs package called ‘atob’ library in order to generate a blob like object from a buffer encoded as a base64 string that I get from doing a fs.readFileSync(file_path)
Here is my code:
/* eslint-disable no-async-promise-executor */
const Blob = require('cross-blob');
const atob = require('atob');
const fetch = require('cross-fetch').default;
const FormData = require('form-data');
const utils_api_merge = async (file, xfdf) => {
const formData = new FormData();
formData.append('file', file);
formData.append('xfdf', xfdf);
formData.append('license', 'my_server_key');
try {
const fetch_resp = await fetch('https://api.pdfjs.express/xfdf/merge', {
method: 'post',
body: formData,
headers: JSON.stringify(formData.getHeaders())
});
if (fetch_resp.status >= 400) {
throw new Error('Bad response from server on merging file');
}
const response = await fetch_resp.json();
const { url, key, id } = response;
console.log(response, 'RESP');
const mergedFileBlob = await fetch(url, {
headers: {
Authorization: key
}
});
if (mergedFileBlob.status >= 400) {
throw new Error('Bad response from server on getting file');
}
return await mergedFileBlob.blob();
} catch (e) {
return e;
}
};
const merge_edits = async (doc, base64_file) => {
return new Promise(async (resolve, reject) => {
try {
const fileData = base64ToBlob(base64_file.toString('base64'));
const xfdf = doc.xfdf_string.split('<?xml version="1.0" encoding="UTF-8" ?>')[1];
const resp = await utils_api_merge(fileData, xfdf);
console.log(resp.arrayBuffer(), 'FILE DATA AFTER MERGE');
resolve(Buffer.from(resp.arrayBuffer()).toString('base64'));
} catch (e) {
console.log(e, 'ERROR IN MERGE EDITS');
reject(e);
}
});
};
function base64ToBlob(base64) {
const binaryString = atob(base64);
const len = binaryString.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; ++i) {
bytes[i] = binaryString.charCodeAt(i);
}
return new Blob([bytes], { type: 'application/pdf' });
}
exports.merge_edits = merge_edits;
Then I get this error:
TypeError: source.on is not a function
3|pdf_corrector | at Function.DelayedStream.create (pdf_corrector/node_modules/delayed-stream/lib/delayed_stream.js:33:10)
3|pdf_corrector | at FormData.CombinedStream.append (pdf_corrector/node_modules/combined-stream/lib/combined_stream.js:45:37)
3|pdf_corrector | at FormData.append (pdf_corrector/node_modules/form-data/lib/form_data.js:75:3)
3|pdf_corrector | at utils_api_merge (pdf_corrector/utils/mergeEditsOnDoc.js:10:12)
3|pdf_corrector | at pdf_corrector/utils/mergeEditsOnDoc.js:51:26
3|pdf_corrector | at new Promise (<anonymous>)
3|pdf_corrector | at merge_edits (pdf_corrector/utils/mergeEditsOnDoc.js:47:10)
3|pdf_corrector | at pdf_corrector/routes/medias/index.js:394:38
3|pdf_corrector | at Layer.handle [as handle_request] (pdf_corrector/node_modules/express/lib/router/layer.js:95:5)
3|pdf_corrector | at next (pdf_corrector/node_modules/express/lib/router/route.js:137:13) ERROR IN MERGE EDITS
Thank you for your time