0

I am running gotenberg as my document to PDF conversion API and it is working with cURL.

The cURL command looks like this

curl --request POST --url http://example.com  --header 'Content-Type: multipart/form-data' --form files=@file.docx > result.pdf

The API works only with cURL, When i try hitting the same API with Postman or AJAX i get a response but on saving the details or by previewing the response using Postman i get an empty PDF file.

My AJAX request looks like this

  var settings = {
        "async": true,
        "crossDomain": true,
        "url": "http://convertionapi.com",
        "method": "POST",
        "processData": false,
        "contentType": false,
        "data": form,
        success: function(data){
            console.log("Success", data);
            s3.upload({
                Key: "files/test.pdf",
                Body: data,
                ContentType: 'application/pdf',
                ACL: 'public-read',
            },function(err, data) {...}
    },
    error: function(err) { 
        console.log("Error", err);
    }
}

Can anyone throw some light as to what is happening with my request?

I get the below headers with my response but the created files are empty

enter image description here

KOMODO
  • 72
  • 1
  • 8
Nithin
  • 1,387
  • 4
  • 19
  • 48
  • 1
    What are you getting in the console.log, when you write "Success" + data? – Brank Victoria Nov 22 '18 at 09:06
  • I am getting like this https://imgur.com/a/hfPQdWB – Nithin Nov 22 '18 at 09:22
  • 1
    Then, it seems that you are getting correctly the PDF from the ajax request. Now what you need to do with that information? Download it? – Brank Victoria Nov 22 '18 at 09:24
  • I am trying to save the PDF to S3 and a PDF is being created on S3, but the entire PDF is just one empty file, by empty file i mean there is no content just plain A4 sheets :/ – Nithin Nov 22 '18 at 09:25
  • I have no idea of what S3 is. Just in case, are you able to see the size of the file created in S3? Is it 51563 bytes? Or it is 0bytes? – Brank Victoria Nov 22 '18 at 09:27
  • Also what I see it is different is that in the CURL you have set the Content-Type header, try to set it in your ajax request. Also, I am not sure if you are passing correctly the data in the variable "form" – Brank Victoria Nov 22 '18 at 09:30
  • Size is correct and I am able to see some part of the content in the raw part of Postman but the files are just blank. It's amazon S3. The file is like this https://imgur.com/a/5sIvlNJ – Nithin Nov 22 '18 at 09:30
  • Set that header as well. Still the same. – Nithin Nov 22 '18 at 09:31
  • Then it seems that you are not doing it well the request. Try to correct it based on what I have just said in previously comments. – Brank Victoria Nov 22 '18 at 09:31
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/184078/discussion-between-brank-victoria-and-nithin). – Brank Victoria Nov 22 '18 at 09:34

1 Answers1

1

I needed to handle blob response and thus my AJAX call must have looked like this.

var settings = {
        "async": true,
        "crossDomain": true,
        "url": "http://convertionapi.com",
        "method": "POST",
        "processData": false,
        "contentType": false,
        "data": form,
        "xhr": function(){
               var xhr = new XMLHttpRequest();
               xhr.responseType= 'blob'
               return xhr;
        },
        success: function(data){
            console.log("Success", data);
            s3.upload({
                Key: "files/test.pdf",
                Body: data,
                ContentType: 'application/pdf',
                ACL: 'public-read',
            },function(err, data) {...}
    },
    error: function(err) { 
        console.log("Error", err);
    }
}
Nithin
  • 1,387
  • 4
  • 19
  • 48