2017-12-04 8 views
0

Axios経由でGoogle APIにPDFを送信する際に問題が発生しました。サーバ上のAxios経由でGoogle APIを送信する(NodeJs)

コードは、私が試した:

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が文字列としてする必要があるため[OK]を、私は私の問題を解決し、エラーが発生しました:、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'
関連する問題