0

我正在尝试从我的远程方法返回一个文件:

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接收已损坏。我在这里做错了什么?任何帮助将不胜感激。我使用的包是html-pdf-node

4

1 回答 1

0

我正在使用不同的包来呈现 pdf (jsreport),但标题不应有所不同:

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

尝试添加“内容传输编码”之一。

于 2021-12-20T08:12:17.017 回答