简体   繁体   中英

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. The content-type in the response header of the API is "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. How can I do this on UI. Do we have any JS library which would help me with this ? I have looked into JSPdf and PDF.js libraries but not sure whether these are the ones I can use.

You can use pdfmake its a great library and you can open, download, and print pdf. check it out pdfmake

The steps

First you define the content of the pdf in a certain JSON format:

    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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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