Merging XFDF using the Express REST API

I have tried Merging XFDF using the Express REST API but getting below error when submitting save button.
{ message: “Invalid input.”, type: “InvalidPDF”, code: 20 }

This is my code

WebViewer({
    path: 'lib', // path to the PDFTron 'lib' folder on your server
    licenseKey: 'Insert license key here', // optional
   // initialDoc: 'https://pdftron.s3.amazonaws.com/downloads/pl/webviewer-demo.pdf',
    initialDoc: 'test.pdf',  // You can also use documents on your server
}, document.getElementById('viewer'))
    .then(function(instance) {
        var docViewer = instance.docViewer;
        var annotManager = instance.annotManager;

        instance.setHeaderItems(function(header) {
            header.push({
                type: 'actionButton',
                img: '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M0 0h24v24H0z" fill="none"/><path d="M17 3H5c-1.11 0-2 .9-2 2v14c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2V7l-4-4zm-5 16c-1.66 0-3-1.34-3-3s1.34-3 3-3 3 1.34 3 3-1.34 3-3 3zm3-10H5V5h10v4z"/></svg>',
                onClick: function() {
                    var docViewer = instance.docViewer;
                    var annotManager = instance.annotManager;
                    var xfdf = annotManager.exportAnnotations({ links: false, widgets: false });
                    var fileData = docViewer.getDocument().getFileData({});

                    var data = new FormData();
                    data.append('xfdf', xfdf);
                    data.append('file', fileData);
                    data.append('license','');

                    console.log(data);

                    var  response =  fetch('https://api.pdfjs.express/xfdf/merge', {
                        method: 'post',
                        body: data
                    }).then(function(resp) {
                        resp.json()
                    });

console.log(response);
                }
            });
        });
    });

Please help

Hi there,

getFileData returns a promise, so you will need to wait for that to resolve before you can use it.

Also you will want to create a blob with the result from that function.

Something like this:

var fileData = await docViewer.getDocument().getFileData({});
var blob = new Blob([fileData], {type: 'application/pdf'})

data.append('file', blob)

Hope this helps!
Logan

1 Like

Hi Logan,

Yes now its working.But i have changed 1.75 mb pdf file and edit some content and requested to merge api.But i got error .“Internal server error” .Also when i try above 3mb pdf file igot another error HTTP content length exceeded 10485760 bytes.

Moved to Internal server error