Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular jspdf [atob] problem with custom font (greek characters)

Tags:

angular

jspdf

I try to generate an pdf from the service with greek characters. on my local machine it's work's good. but on real server I'm getting error

sPDF PubSub Error Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded. DOMException: Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.

.ttf file is in service folder

I call this function from my component. service code is:

...//some imports

public printSomeData(data: any) {
    // getting data to the parse

      doc.addFileToVFS('Adria Slab W00 Medium.ttf')
      doc.addFont('Adria Slab W00 Medium.ttf', 'Adria Slab W00 Medium', 'normal')
      doc.setFont('Adria Slab W00 Medium', 'sans-serif')


      doc.setFontSize(data.fontSize)

      let body = []
      // generate body code

      autoTable(doc,{
        head: [head],
        body: body,
        tableLineColor: [189, 195, 199],
        tableLineWidth: 0.75,
        theme: 'grid',
        // headStyles: {co},
        bodyStyles: {lineColor: [0, 0, 0]},

        styles: {
          font: 'Adria Slab W00 Medium',    // <-- custom font
          fontStyle: 'normal',
          fontSize: data.fontSize,
          overflow: 'linebreak'
        },

      })

      doc.save('somename.pdf');

    }
  })
)

  }
like image 351
Mike Avatar asked Nov 16 '25 19:11

Mike


1 Answers

  1. generate js file from the .ttf font file(use font converter online)

  2. create font.ts file in your service folder

  3. export const fontString : string ="AAEAAA"; <-- paste all string from generated file here

      import {fontString} from "./font";
      doc.addFileToVFS("AdriaSlabW00Medium.ttf", fontString);
      doc.addFont("AdriaSlabW00Medium.ttf", "Adria Slab W00 Medium", "normal");
      doc.setFont('Adria Slab W00 Medium', 'normal');
    
like image 195
Arams Avatar answered Nov 19 '25 10:11

Arams