1

My requirement is to generate pdf view of UI(angular 4 app) using Nodejs api. For that i fetched the entire content from UI and did some pre-processing which will be send to nodejs api. In node api i used html-pdfpackage to generate pdf from the html received. Am able to generate pdf with proper styling as it appears in UI. Below are my questions

  • What is the safe way to pass the entire UI content(html, styles, bootstrap css) to Node api. (Currently passing as normal string for POC purpose)
  • How will i return the pdf stream generated by html-pdfpackage back to UI to show it on new tab

html-pdf package

Node api call from angular:

  this.http.post('http://localhost:3000/api/createpdf', { 'arraybuff': data }, options)
        .catch(this.handleError)
        .subscribe(res => {

        })

Currently data is normal html string, which i am retrieving by setting body-parser in node.

Satheesh
  • 853
  • 2
  • 15
  • 36

1 Answers1

0

I don't think there is any better way to pass HTML to server or may be I am not aware of it, but to open the link in new tab use below code

 this.http.post('http://localhost:3000/api/createpdf', { 'arraybuff': data }, options)
.catch(this.handleError)
.subscribe(res => {
       var blob = new Blob([(<any>res)], { type: 'application/pdf' });
       var url = window.URL.createObjectURL(blob);
       window.open(url);
})
Nitishkumar Singh
  • 1,644
  • 11
  • 32
  • but this will create a blob url. Is that secure? I tried this already and by looking at url (blob:https://....) just hesitate to do so. – Satheesh Apr 24 '18 at 23:00
  • This blob url will be local to client machine. Since you are generating it on client machine. I don't find any issue into that. You can also send an url with validity period or something. – Nitishkumar Singh Apr 25 '18 at 03:55