0

I have problems trying to send a PDF to Google API via Axios.

Code I tried:

const express = require('express');
const axios = require('axios');
const router = express.Router();
const PDFDocument = require('pdfkit');

const ticketProperties = {
  'version': '1.0',
  'print': {}
};

router.post('/print/sale', async (req, res) => {

    // Generate test PDF
    let doc = new PDFDocument;
    doc.pipe(fs.createWriteStream('output.pdf'));
    doc.fontSize(8)
      .text('Some text example for pdf', 1, 1);
    doc.end();

    // Once the pdf is generated, read it and send it via axios
    axios.post(
      'https://www.google.com/cloudprint/submit',
      {
        printerid : 'PRINTER_ID_REMOVED_INTENTIONALLY',
        title: 'pdf print',
        ticket: ticketProperties,
        content : doc,
        contentType: 'application/pdf'
      },
      {
        headers: {
        'Authorization': 'Bearer ACCESS_TOKEN_REMOVED_INTENTIONALLY',
        'Content-Type': 'application/pdf'
        }
      }
    )
      .then(response => {
        console.log(response);
      })
      .catch(error => {
        console.log(error);
      });

});

The error I got in console.log(error) when I call that API:

TypeError: Converting circular structure to JSON at JSON.stringify () at transformRequest (G:\projects\pos-web\pos-backend\node_modules\axios\lib\defaults.js:51:19) at transform (G:\projects\pos-web\pos-backend\node_modules\axios\lib\core\transformData.js:16:12) at Object.forEach (G:\projects\pos-web\pos-backend\node_modules\axios\lib\utils.js:224:12) at transformData (G:\projects\pos-web\pos-backend\node_modules\axios\lib\core\transformData.js:15:9) at dispatchRequest (G:\projects\pos-web\pos-backend\node_modules\axios\lib\core\dispatchRequest.js:37:17) at

I think that error is because I'm writing the file doc inside the body of post. Do I need to process it before?

Notes:

  1. The pdf is generated successfully inside of my project.
  2. My first test was sending a text instead of pdf and was OK.
  3. I've tried a lot of possible solutions, but give me different errors, maybe I'm understanding wrong how to send a pdf from the server. Please If you have another library instead of axios or you know the flow of how to send a pdf from the server, don't hesitate in write it, I'll take it as a good answer.

More information:

  • about endpoint https://www.google.com/cloudprint/submit : Link

1 Answers1

0

Ok, I solved my problem, the error was because ticketProperties need to be as string: JSON.stringify(ticketProperties), because it is an object.

Despite I solved this problem, I had others problems with Axios package and I decided change axios by request package to make my request API and It worked. This solution helped me: Stackoverflow link

code:

var formData = {
  printerid : 'PRINTER_ID_REMOVED_INTENTIONALLY',
  title: 'pdf print',
  ticket: JSON.stringify(ticketProperties),
  content: fs.createReadStream('output.pdf'),
  contentType: 'application/pdf'
};

request.post(
  {
    url:'https://www.google.com/cloudprint/submit',
    formData: formData,
    headers: {
      'Authorization': 'Bearer ACCESS_TOKEN_REMOVED_INTENTIONALLY'
    }
  },
  function optionalCallback(err, httpResponse, body) {
    if (err) {
      console.error('upload failed:', err);
    } else {
      console.log('Upload successful!  Server responded with:', body);
    }
  }
);

But After It worked, I had more problems with pdf:

  1. I need to wait that pdf will be generated with async function doc.end() before calling to fs.createReadStream('output.pdf'), otherwise I'll have the error could not convert to pdf

  2. Great upper margin in printing, impossible to reduce it.

After spending hours trying to solve this, I read that is possible send HTML data and I think that is better because it consumes fewer resources and solves the problems described.

Only I changed:

  • content: '<MY_STRING_HTML>'
  • contentType: 'text/html'