简体   繁体   English

如何使用从 REST API 返回的 PDF 内容创建 PDF 文档

[英]how to create a PDF doc with the PDF content returned from a REST API

I am calling a REST API from the browser which gives me just the PDF content.我正在从浏览器调用 REST API,它只给我 PDF 内容。 The content-type in the response header of the API is "application/pdf". API 响应头中的内容类型是“application/pdf”。

I need to create a PDF document with this content and I should be able to open the PDF doc in a new tab and I should be able to download the PDF as well.我需要创建一个包含此内容的 PDF 文档,我应该能够在新选项卡中打开 PDF 文档,我也应该能够下载 PDF。 How can I do this on UI.我怎样才能在 UI 上做到这一点。 Do we have any JS library which would help me with this ?我们是否有任何 JS 库可以帮助我解决这个问题? I have looked into JSPdf and PDF.js libraries but not sure whether these are the ones I can use.我已经研究过 JSPdf 和 PDF.js 库,但不确定这些是否是我可以使用的。

You can use pdfmake its a great library and you can open, download, and print pdf.您可以使用pdfmake它一个很棒的库,您可以打开、下载和打印 pdf。 check it out pdfmake检查出来pdfmake

The steps步骤

First you define the content of the pdf in a certain JSON format:首先,您以某种 JSON 格式定义 pdf 的内容:

    var docDefinition = {
  content: [
    { text: 'This is a header', style: 'header' },
    'No styling here, this is a standard paragraph',
    { text: 'Another text', style: 'anotherStyle' },
    { text: 'Multiple styles applied', style: [ 'header', 'anotherStyle' ] }
  ],

  styles: {
    header: {
      fontSize: 22,
      bold: true
    },
    anotherStyle: {
      italics: true,
      alignment: 'right'
    }
  }
};

download下载

pdfMake.createPdf(docDefinition).download();

open打开

var win = window.open('', '_blank');
pdfMake.createPdf(docDefinition).open({}, win);

print打印

pdfMake.createPdf(docDefinition).print();

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

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