0

I am using angular 2 for my projects. I have my template ready for print. I want to reuse the same for my pdf also with same css. So i wrote below code to pick up the html

parseHTML(html) {
        return new DOMParser().parseFromString(html, 'text/html');
    }

  this._http.get('assets/print-template.html').toPromise()
              .then((response: any) => {
                  let mynewnodes = me.parseHTML(response._body)
                  mynewnodes.head.innerHTML = mynewnodes.head.innerHTML + AllStyleSheets;
                  mynewnodes.getElementById('holder').innerHTML = printContents;
                  const source = mynewnodes.getElementsByTagName('html')[0].outerHTML;
});

It basically gets html from local file and makes it as a dom object and add css and dynamic content to it. I want to print the source html string that i have as a pdf. I tried jspdf but could not get it working fine.

I found below http://blog.stahlmandesign.com/export-html-to-pdf-how-hard-can-it-be/ Not sure which one to use. Basic requirement is to print static html in a variable with html and css to a pdf and export.

Hacker
  • 7,798
  • 19
  • 84
  • 154

1 Answers1

0

Have you considered using an Iframe, you could use the 'srcdoc' tag to reference HTML and the 'type' tag to create it as a PDF:

<iframe srcdoc="<p>Hello world!</p>" src="demo_iframe_srcdoc.htm" type="application/pdf"></iframe>

and you could style this to fill the whole screen with something like:

<style>html, body { padding: 0; margin: 0; } iframe { width: 100%; height: 100%; border: 0;}  </style>'