import { Injectable } from '@angular/core' ; import { Company } from '../models/Company' ; import { Letter } from '../models/Letter' ; const jsPDF = require ( 'jspdf' ) ; @ Injectable ( ) export class LetterService { param { Company } company param { Letter } letter param { String } sender param { String } txt public createPdf ( company , letter , sender , txt = null ) { const doc = new jsPDF ( { orientation : 'portrait' , unit : 'mm' } ) ; doc . setFontSize ( 14 ) ; doc . text ( letter . titleText , 20 , 100 ) ; doc . setFontSize ( 12 ) ; doc . text ( this . getDate ( ) , 170 , 110 ) ; doc . text ( doc . splitTextToSize ( sender , 40 ) , 140 , 20 ) ; doc . setLineWidth ( 180 ) ; doc . setFontSize ( 12 ) ; const recipient = company . companyName + '\n' + ( company . companyAddress ? company . companyAddress : '' ) + '\n' + ( company . companyZip ? company . companyZip + ' ' : '' ) + ( company . companyCity ? company . companyCity : '' ) + '\n' + ( company . companyCountry ? company . companyCountry : '' ) ; doc . text ( recipient , 20 , 60 ) ; if ( ! txt ) { if ( letter . rawText ) { txt = letter . rawText ; } else { txt = this . generateText ( company , letter , sender ) ; } } const lines = doc . splitTextToSize ( txt , 170 ) ; let y = 120 ; for ( const line of lines ) { doc . text ( line , 20 , y ) ; y += 5 ; if ( y > 270 ) { doc . addPage ( ) ; y = 20 ; } } const fileName = company . companyName . replace ( /[^A-Za-z0-9]/ , '_' ) ; doc . save ( fileName + '.pdf' ) ; } public generateEmail ( company , letter , sender , txt = null ) { if ( ! txt ) { if ( letter . rawText ) { txt = letter . rawText ; } else { txt = this . generateText ( company , letter , sender ) ; } return 'mailto:' + company . companyEmail + '?subject=' + encodeURIComponent ( letter . name ) + '&body=' + encodeURIComponent ( txt ) ; } } param { Company } company param { Letter } letter param { String } sender public generateText ( company , letter , sender ) { const senderName = this . getFirstLine ( sender ) ; let txt = '' ; if ( company . contractNumber ) { txt += 'Betreff: ' + company . contractNumber + '\n\n' ; } txt += letter . introText ; for ( const textBlock of letter . textblocks ) { if ( textBlock . selected === true ) { txt += ' - ' + textBlock . content + '\n' ; } } const lastChar = txt . substr ( txt . length - 2 ) ; if ( lastChar === ',\n' ) { txt = txt . substr ( 0 , txt . length - 2 ) + '.\n' ; } if ( company . remarks ) { txt += '\n\n' + company . remarks + '\n' ; } txt += letter . endClause + '\n\n\n' ; txt += senderName ; return txt ; } public getFirstLine ( sender ) { const senderLines = sender . split ( '\n' ) ; return senderLines [ 0 ] ; } returns { string } public getDate ( ) { const today = new Date ( ) ; let dd = today . getDate ( ) . toString ( ) ; let mm = ( today . getMonth ( ) + 1 ) . toString ( ) ; const yyyy = today . getFullYear ( ) ; if ( parseInt ( dd , 10 ) < 10 ) { dd = '0' + dd ; } if ( parseInt ( mm , 10 ) < 10 ) { mm = '0' + mm ; } return dd + '.' + mm + '.' + yyyy ; } constructor ( ) { } }