kempsteven / vue-html2pdf

vue-html2pdf converts any vue component or element into PDF, vue-html2pdf is basically a vue wrapper only and uses html2pdf.js behind the scenes.
https://www.npmjs.com/package/vue-html2pdf
MIT License
440 stars 74 forks source link

Add custom fonts #45

Closed lincolnlemos closed 4 years ago

lincolnlemos commented 4 years ago

I'm using the beforeDownload event to create extra pages on top of the document PDF generated by the vue-html2pdf

My issue is that I'm not able to add custom fonts. I'm importing the file generated by the jsPDF documentation but It's not working.

Anyone made it work?

lincolnlemos commented 4 years ago

Solved create an export function from the generated file directly to the jsPDF document.

Example:

// Code generated by [their font converter](https://rawgit.com/MrRio/jsPDF/master/fontconverter/fontconverter.html)
import { jsPDF } from "jspdf"
const font = 'AAEAAAAQAQAAB....'

var callAddFont = function () {
  this.addFileToVFS('Lato-Regular-normal.ttf', font);
  this.addFont('Lato-Regular-normal.ttf', 'Lato-Regular', 'normal');
};
jsPDF.API.events.push(['addFonts', callAddFont])

// Added the export function so I can add the custom fonts at my document
export const registerLatoRegular = (doc) => {
  doc.addFileToVFS('Lato-Regular-normal.ttf', font);
  doc.addFont('Lato-Regular-normal.ttf', 'Lato-Regular', 'normal');
}