簡體   English   中英

從 Remote 方法 Loopback 返回的 PDF 已損壞

[英]PDF returned from Remote method Loopback is corrupted

我正在嘗試從我的遠程方法返回一個文件:

Visitor.remoteMethod('getVisitorsPDF', {
    description: 'Get visitors list in PDF file',
    accepts: [
      { arg: 'res', type: 'object', http: { source: 'res' } },
      { arg: 'dateInStart', type: 'string' },
      { arg: 'dateInEnd', type: 'string' },
      { arg: 'employeeSiteId', type: 'string' },
      { arg: 'name', type: 'string' },
      { arg: 'visitorCompany', type: 'string' },
      { arg: 'employeeName', type: 'string' },
      { arg: 'typeId', type: 'number' },
      { arg: 'shift', type: 'number' },
    ],
    returns: {},
    http: { path: '/getpdf', verb: 'get' },
  });

生成 pdf 的代碼是:

res.set('Content-Type', 'application/octet-stream');
res.set('Content-Disposition', 'attachment;filename="visitors.pdf"');
let options = { format: 'A4' };
// Example of options with args //
// let options = { format: 'A4', args: ['--no-sandbox', '--disable-setuid-sandbox'] };

let file = { content: "<h1>Welcome to html-pdf-node</h1>" };
// or //
let file = { url: "https://example.com" };
html_to_pdf.generatePdf(file, options).then(pdfBuffer => {
  console.log("PDF Buffer:-", pdfBuffer);
  res.send(pdfBuffer);
});

但是 pdf 接收已損壞。 我在這里做錯了什么? 任何幫助將不勝感激。 我使用的 package 是html-pdf-node

我正在使用不同的 package 來渲染 pdf (jsreport),但標題不應有所不同:

res.set('Content-Type', renderedData.headers['content-type']);
res.set('Content-Disposition', renderedData.headers['content-disposition']);
res.set('Content-Transfer-Encoding', 'binary');

嘗試添加“內容傳輸編碼”之一。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM