Download pdf webpage using corner 4

I am new to angular 4. I need to download an HTML page in pdf format and the html web page contains angular controls like input [(ngModel)] , radio button [verified] . Instead of showing control values, it shows undefined in a pdf file.

I tried using jsPdf and html2Pdf , but it throws an error

Error:

You need either https://github.com/niklasvh/html2canvas or https://github.com/cburgmer/rasterizeHTML.js
    at Object.jsPDFAPI.addHTML (jspdf.debug.js?6964:4049)

Code for TS file:

import { Component, ViewChild, Compiler, ComponentRef, OnInit, EventEmitter, ElementRef } from "@angular/core";
    import { Router } from '@angular/router';
    import { FormGroup, FormBuilder, FormControl, Validators } from '@angular/forms';
    import * as jsPDF from 'jspdf';
    import * as html2canvas from 'html2canvas';


    @Component({
        templateUrl: "./dashboard.html"
    })

    export class DashboardComponent implements OnInit {
     ngOnInit() {

        }


        pdf() {
            let pdf = new jsPDF();
            let options = {
                pagesplit: true
            };
            pdf.addHTML(document.getElementById("htmlform"), 0, 0, options, () => {
                pdf.save("test.pdf");
            });
        }
    }

HTML code

<div>
<button type="button" (click)="pdf()"></button>
</div>
<ng-component>
<html id="htmlform">
<head>
DashBoard
</head>
<body>
<h3>
Person Details
</h3>
<table>
  <tbody>
 <tr class="BorderTopSec">
                <td colspan="2">
                    <div class="label">
                        &nbsp;NAME:
                    </div>
                    <input name="person_name" type="text" [(ngModel)]="model.Name" [ngModelOptions]="{standalone:true}" />
                </td>
                </tr>
                <tr>
                 <td rowspan="4" style="border-width: 1px 1px 1px 0px; border-style: solid solid solid none; border-color: black black black white; width: 164px;">
                    <img height="150" id="imgPhoto" src="" width="155" />
                </td>
                </tr>
                <tr>
                 <td>
                    <div class="label">
                        &nbsp;LOCATION:
                    </div>
                    <select name="LOCATION" style="border: currentColor; width: 95px; font-size: 13px;" [(ngModel)]="model.location">
                        <option *ngFor="let item of model.location" [ngValue]="locationId">{{item.location}}</option>
                    </select>
                </td>
                </tr>
                <tr>
                 <td>
                    <div class="label">
                        &nbsp;SEX:
                    </div>
                    </td>
                    <td>
                <md-radio-group>
  <md-radio-button value="1"   [ngModelOptions]="{standalone:true}" >Option 1</md-radio-button>
  <md-radio-button value="2" [ngModelOptions]="{standalone:true}" >Option 2</md-radio-button>
</md-radio-group>
                </td>

</tr>
</tbody>

</table>

</body>
</html>
</ng-component>
+3
source share
1 answer

/html2canvas/dist/html2canvas.min.js script, , angular-cli.json scripts: [].

import * as jsPDF from 'jspdf'; .

: ng serve, , script.

0

Source: https://habr.com/ru/post/1666537/


All Articles