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:
- The pdf is generated successfully inside of my project.
- My first test was sending a text instead of pdf and was OK.
- 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