2017-12-04 22 views
0

Axios을 통해 Google API로 PDF를 보내려고하는데 문제가 있습니다.서버 (NodeJs)의 axios를 통해 google API로 보내기

코드 나는 시도 :

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); 
     }); 

}); 

내가console.log(error)에있어 오류 난 그 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

내가 파일을 쓰고 있어요 때문 오류가 생각 게시물의 몸 안에 doc. 전에 처리해야합니까?

노트 :

  1. PDF를 내 프로젝트의 내부에 성공적으로 생성됩니다.
  2. 첫 번째 테스트는 pdf 대신 텍스트를 보내고 OK입니다.
  3. 나는 많은 가능한 해결책을 시도했지만, 나에게 다른 오류를 주었다. 어쩌면 서버에서 pdf를 보내는 방법을 잘못 이해하고있을 수도있다. 하시기 바랍니다 Axios 대신에 다른 라이브러리가 있거나 서버에서 pdf를 보내는 방법의 흐름을 알고 있다면 주저하지 말고 답변으로 보내십시오.

더 많은 정보 : 엔드 포인트 https://www.google.com/cloudprint/submit에 대한

답변

0

ticketProperties이 문자열로 할 필요가 있기 때문에 좋아, 내 문제를 해결, 오류 :, JSON.stringify(ticketProperties) 그것 때문에 객체입니다.

나는이 문제를 해결했지만 Axios 패키지에 다른 문제가있어서 axios에 의해 request 패키지가 요청한 API를 작성하고 작동했습니다. 이 솔루션은 나에게 도움이 : Stackoverflow link

코드 :

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); 
    } 
    } 
); 

그러나 그것은 일 후, 나는 PDF와 더 많은 문제 있었다 : 그 PDF 파일이 생성됩니다 기다릴 필요가

  1. 을 비동기 기능 doc.end()을 호출하기 전에 fs.createReadStream('output.pdf')으로 전화하십시오. 그렇지 않으면 오류가 있습니다. could not convert to pdf

  2. 인쇄시 큰 상한 여백을 줄이면 안됩니다.

이 문제를 해결하려고 시간을 소비 한 후, 그 수 보내기 HTML 데이터입니다 읽고 난 적은 자원을 소모하고 설명하는 문제를 해결하기 때문에 그것이 더 나은 생각합니다.

만 나는 변경 :

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