简体   繁体   English

从 Remote 方法 Loopback 返回的 PDF 已损坏

[英]PDF returned from Remote method Loopback is corrupted

I am trying to return a file from my remote method:我正在尝试从我的远程方法返回一个文件:

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

And the code to generate pdf is:生成 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);
});

But the pdf recieve is corrupted.但是 pdf 接收已损坏。 What i am doing wrong here?我在这里做错了什么? any help will be appreciated.任何帮助将不胜感激。 The package i am using is html-pdf-node我使用的 package 是html-pdf-node

I'm using a different package to render pdf (jsreport) but the headers should not differ:我正在使用不同的 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');

Try to add the "Content-Transfer-Encoding" one.尝试添加“内容传输编码”之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM