简体   繁体   English

将来自 api 调用的返回响应保存到 Javascript 中的 PDF

[英]Save return response from api call to a PDF in Javascript

I am making via RingCentral Api and I am getting the below response via a node request.我正在通过 RingCentral Api 进行制作,并且通过节点请求得到以下响应。 I am trying to figure out how I can save the response (which should be PDF source) so that I can then open it.我试图弄清楚如何保存响应(应该是 PDF 源),以便我可以打开它。

This is the response.这是回应。

Response {
  size: 0,
  timeout: 0,
  [Symbol(Body internals)]: {
    body: PassThrough {
      _readableState: [ReadableState],
      readable: true,
      _events: [Object: null prototype],
      _eventsCount: 5,
      _maxListeners: undefined,
      _writableState: [WritableState],
      writable: true,
      allowHalfOpen: true,
      _transformState: [Object],
      [Symbol(kCapture)]: false
    },
    disturbed: false,
    error: null
  },
  [Symbol(Response internals)]: {
    url: 'https://platform.ringcentral.com/restapi/v1.0/account/~/extension/1234/message-store/1234/content/1234?contentDisposition=Inline',
    status: 200,
    statusText: 'OK',
    headers: Headers { [Symbol(map)]: [Object: null prototype] },
    counter: 0
  }
}

This is the code:这是代码:

    let getMessageContent = await platform.get('/restapi/v1.0/account/~/extension/'+accountId+'/message-store/'+attachId+'/content/'+attachId, {
        contentDisposition: ['Inline']
    })
    let jsonMessageContent = await getMessageContent.headers
        console.log(jsonMessageContent)

If I run the URL in post man, I can see the PDF displayed in this way.如果我在 post man 中运行 URL,我可以看到 PDF 以这种方式显示。

In Postman (this is just an example)在 Postman 中(这只是一个例子)

%PDF-1.3
%����
1 0 obj 
<<
/Pages 2 0 R
/Type /Catalog
>>
endobj 
2 0 obj 
<<
/Kids [3 0 R]
/Count 1
/Type /Pages
>>
endobj 
3 0 obj 
<<
/CropBox [0 0 609.882 818.449]
/Parent 2 0 R
/Thumb 4 0 R
/MediaBox [0 0 609.882 818.449]
/Resources 
<<
/XObject 

Any Ideas?有任何想法吗?

When in the past I have needed to create an endpoint that took certain information (some of it from the very same request), produced a PDF file with it, and served it as its response I found PDFkit to be a strong ally.过去我需要创建一个端点来获取某些信息(其中一些来自同一个请求),生成一个 PDF 文件,并将其作为其响应,我发现PDFkit是一个强大的盟友。 The main reason for it is simple: its documentation is more than enough to get the work done.其主要原因很简单:它的文档足以完成工作。 The only problem is that getting that very same job to look slightly ok is rather tedious.唯一的问题是,让同样的工作看起来稍微不错是相当乏味的。 To produce your PDF I would suggest a pdfCreatorMethod() similar to this:要生成您的 PDF 我建议使用类似于此的pdfCreatorMethod()

const PDFDocument = require("pdfkit");
const fs = require("fs");

const doc = new PDFDocument({ autoFirstPage: false });

doc.pipe(fs.createWriteStream('PATH/TO/YOUR/PDF'));

doc.registerFont("Helvetica", "client/assets/fonts/Helvetica-01.ttf");
doc.registerFont(
"HelveticaBold",
"client/assets/fonts/Helvetica-Bold-02.ttf"
);

doc.font("Helvetica").fontSize(10).fillColor("#80B3BD").text("", 70, 80, {
width: 100,
align: "left",
lineBreak: true,
});

/**
 * ADD WHATEVER YOU WANT TO YOUR FILE
 */

doc.end();

Allowing you to include any information that you desire in the document.允许您在文档中包含您想要的任何信息。 After this, you will only need your route to serve the path to the file you created:在此之后,您将只需要您的路线来提供您创建的文件的路径:

router.put("/mypdf", async (req, res) => {
    try {
      pdfCreatorMethod(req);
      res.set("Content-Type", "application/pdf");
      return res.status(201).sendFile('PATH/TO/YOUR/PDF');
    } catch (e) {
      console.error(e);
      return res.status(500).send({ error: e.message });
    }
});

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

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