Advertisement
maurol22

Untitled

Mar 10th, 2021
820
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, OnInit } from '@angular/core';
  2. import {PDFDocument, PDFForm} from "pdf-lib";
  3. import {HttpClient, HttpHeaders} from "@angular/common/http";
  4.  
  5. @Component({
  6.   selector: 'app-pdf-form',
  7.   templateUrl: './pdf-form.component.html',
  8.   styleUrls: ['./pdf-form.component.css']
  9. })
  10. export class PdfFormComponent implements OnInit {
  11.   formFields = [];
  12.   private form: PDFForm;
  13.  
  14.   constructor(
  15.     private http: HttpClient
  16.   ) { }
  17.  
  18.   ngOnInit(): void {
  19.     this.loadForm();
  20.   }
  21.  
  22.   async loadForm(){
  23.     // Fetch the PDF with form fields
  24.     const formUrl = '../../assets/modello_autodichiarazione_editabile_ottobre_2020.pdf';
  25.     const formPdfBytes = await this.http.get(formUrl, {responseType: "arraybuffer"}).toPromise();
  26.     // const formPdfBytes = await fetch(formUrl).then(res => res.arrayBuffer());
  27.  
  28.     // Load a PDF with form fields
  29.     const pdfDoc = await PDFDocument.load(formPdfBytes);
  30.  
  31.     // Get the form containing all the fields
  32.     this.form = pdfDoc.getForm();
  33.  
  34.     const fields = this.form.getFields()
  35.     fields.forEach(field => {
  36.       const type = field.constructor.name
  37.       const name = field.getName()
  38.       this.formFields.push({type: type, name: name});
  39.     })
  40.   }
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement