klabautermann123
5/5/2017 - 7:57 AM

Javascript generate PDF-Preview using FileSaver.js

Javascript generate PDF-Preview using FileSaver.js

 submitDataToSTRS(data: Belegdaten, printToPrinter: boolean): Observable<any> {

    console.log('Für STRS ->', data);

    let date = moment(new Date()).format("DD.MM.YYYY HH:mm");

    let outPutType = 1; //Drucker
    if (!printToPrinter) {
      outPutType = 0; //PDF
    }


    // Prepare Data to be send
    let xml = '';
    xml = '<event docid="Unterschrift" Kunden_System="HE" Version="1.0" ertellt_am="' + date + '">';
    xml += '<Belegdaten>';
    xml += '<output_type>' + outPutType + '</output_type>';
    xml += '<User_Name_Ersteller>' + data.User_Name_Ersteller + '</User_Name_Ersteller>';
    xml += '<Drucker>' + data.Drucker + '</Drucker>';
    xml += '<User_Name_Unterschrift>' + data.User_Name_Unterschrift + '</User_Name_Unterschrift>';
    xml += '<User_Name_Vorname>' + data.User_Name_Vorname + '</User_Name_Vorname>';
    xml += '<User_Name_Nachname>' + data.User_Name_Nachname + '</User_Name_Nachname>';
    xml += '<User_Name_SMTP>' + data.User_Name_SMTP + '</User_Name_SMTP>';
    xml += '<User_Name_Telefon>' + data.User_Name_Telefon + '</User_Name_Telefon>';
    xml += '<Aenderungsgrund>1</Aenderungsgrund>'; //fix
    xml += '<Aenderungsdatum>' + data.Aenderungsdatum + '</Aenderungsdatum>';
    xml += '</Belegdaten>';
    xml += '</event>';

   

    //-----------------------------------------------
    // Init Request-Headers and expect Data as Blob
    //----------------------------------------------
    let requestOptions = new RequestOptions();
    requestOptions.responseType = ResponseContentType.Blob;

   //-----------------------
   // Send the request
   //-----------------------
    return this.http.post(this.config.__strsPostURL, xml, requestOptions)
      .catch(this.handleError);
  };

/*
  Javascript generate PDF-Preview using FileSaver.js
  
   private fileSaver = require('../../../node_modules/file-saver/FileSaver.js');

*/


  private fileSaver = require('../../../node_modules/file-saver/FileSaver.js');
  
  printPDF() {
    this.submitted = true;
    let belegDaten = this.getBelegDataToBeSubmitted();
    
    // calls submitDataToSTRS
    this.pdfSubscription = this.sendDataToStreamServe(belegDaten, false).subscribe(data => {
      let blob: Blob = new Blob([(data as Blob)], {type: 'application/pdf'});
      this.fileSaver.saveAs(blob, 'formular_vorschau.pdf');
    });
  }