sphilee / jsPDF-CustomFonts-support

This repo is being deprecated. Please check out https://github.com/MrRio/jsPDF
https://github.com/MrRio/jsPDF
MIT License
183 stars 77 forks source link

Plan on adding custom fonts? (Feature Request) #25

Open HackbrettXXX opened 6 years ago

HackbrettXXX commented 6 years ago

Hello there,

first of all, thanks for this great effort to add arbitrary fonts to jsPDF. We are currently integrating this plugin into our fork https://github.com/yWorks/jsPDF in order to be able to use custom fonts with svg2pdf.js.

We are quite happy with the progress this project is making recently. However, we miss the functionality to comfortably add custom fonts. Either at runtime on the browser or with a small build tool that generates a file like "defaultVFS.js" from font files. What would also be great is support for other font formats like WOFF/WOFF2 or SVG-Fonts.

What are your plans concerning this features?

Greetings, Hackbrett

HackbrettXXX commented 6 years ago

I just saw the VFS-generation tool. Great!

For WOFF/SVG conversion this package might come in handy: https://www.npmjs.com/package/fonteditor-core

sphilee commented 6 years ago

@HackbrettXXX I applied the code to the package you gave me. Applying this code seems to be usable for pdf conversion except for some font formats. However, the big problem is that 'fonteditor-core' takes too long.

const Font = require('fonteditor-core').Font;
const fs = require('fs');

const path = './fonts/';
fs.readdir(path, (err, files) => {
    if (err) console.log(err);

    const fontList = files.map(file => {
        const format = file.split('.')[1];
        const base64Conetent = Font.create(fs.readFileSync(path + file), {
            type: format
        }).toBase64({
            type: 'ttf'
        }).split(',')[1];
        return `jsPDFAPI.addFileToVFS('${file}','${base64Conetent}');`;
    });
    fs.writeFileSync('./dist/default_vfs.js', `(function (jsPDFAPI) {\n${fontList.join('\n')}\n})(jsPDF.API);`);
})
HackbrettXXX commented 6 years ago

@sphilee Yeah, I tested it myself and it didn't work for the fonts I tested.

I made some small improvements: #28