Skip to main content
Participant
December 21, 2021
Answered

PDF Compression using Rest API

  • December 21, 2021
  • 1 reply
  • 2905 views

I have been attempting to compress PDFs using the rest api POST call - my issue is that no matter how I format the InputFile0, form-data field - the subsequent GET api call is saying that my file is corrupted however I am 100% confident that I am passing a valid PDF file - I have been attempting to compare results using postman on the same file, which works, but setting this field programmatically there is something I am missing:  I have tried all the formats in the FileReader object namely:  

readAsBinaryString, 
readAsDataURL, 
readAsText,
readAsArrayBuffer
None of them resolve the error - how is anyone else programmaticaly setting InputFile0 such the the server does not complain about a corrupted file ?
This topic has been closed for replies.
Correct answer Stuart22022405d216

I found the problem - so this thread is resolved -- thank u

1 reply

Raymond Camden
Community Manager
Community Manager
December 22, 2021

Could you share the code you are using, and possibly the PDF as well? (Note, Adobe is about to go on holiday break so we may be slow to respond.)

Participant
December 23, 2021

Sure - the variable finalPdf is 100% a valid pdf file and I have attached the pdf file:  here is the code:

 

            reader.onload = function(ev) {
                var formData = new FormData();
                               
                formData.append('contentAnalyzerRequests',
                    JSON.stringify(
                        {
                            "cpf:inputs": {
                                "params": {
                                    "cpf:inline": {
                                        "compressionLevel": "HIGH"
                                    }
                                },
                                "documentIn": {
                                    "cpf:location": "InputFile0",
                                    "dc:format": "application/pdf"
                                }
                            },
                            "cpf:engine": {
                                "repo:assetId": "urn:aaid:cpf:Service-f37d36f4e6724eed92149a8ff35ea061"
                            },
                            "cpf:outputs": {
                                "documentOut": {
                                    "cpf:location": "multipartLabelOut",
                                    "dc:format": "application/pdf"
                                }
                            }
                        }                        
                    ));
//&?
debugger
                formData.append('InputFile0', ev.target.result);

                $.ajax({
                    type: 'POST',
                    cache: false,
                    data: formData,
                    processData: false,
                    contentType: false,
                    enctype: 'multipart/form-data',
                    headers: {
                        'Prefer': 'respond-async,wait=0',
                        'Authorization': 'Bearer ' + APIKEY,
                        'Accept': 'application/json, text/plain, */*',
                        'x-api-key': 'f05e4d2e20fb43b3bf390925a1e3253e',
                    },

                    success: function(res, stat, xhr) {
//&?
debugger
                        $.ajax({
                            type: 'GET',
                            processData: false,
                            contentType: false,
                            url: xhr.getResponseHeader('location'),
                            headers: {
                                'Authorization': 'Bearer ' + APIKEY,
                                'x-api-key': 'f05e4d2e20fb43b3bf390925a1e3253e',
                            },

                            success: function(data) {
//&?
debugger
                            }
                        });
                    }
                });

            };


            var pdffileBlobContract = new Blob(new Uint8Array(finalPdf), {type: 'application/pdf'});

            reader.readAsArrayBuffer(pdffileBlobContract);
Stuart22022405d216AuthorCorrect answer
Participant
December 23, 2021

I found the problem - so this thread is resolved -- thank u