Jspdf: setFont does not work on local server

Created on 22 May 2015  路  9Comments  路  Source: MrRio/jsPDF

I am looking foward to use jsPDF, but I found a possible problem with setFont function.

I've just cloned the git repository to my machine and started executing the basic.html example to check it out.

Inside the example there is a code to test different font types

var doc = new jsPDF();

doc.text(20, 20, 'This is the default font.');

doc.setFont("courier");
doc.text(20, 30, 'This is courier normal.');

doc.setFont("times");
doc.setFontType("italic");
doc.text(20, 40, 'This is times italic.');

doc.setFont("helvetica");
doc.setFontType("bold");
doc.text(20, 50, 'This is helvetica bold.');

doc.setFont("courier");
doc.setFontType("bolditalic");
doc.text(20, 60, 'This is courier bolditalic.');

doc.save('Test.pdf');

It works fine if I test on github using https://mrrio.github.io/jsPDF/examples/basic.html.

It does not work if I test it using my local addresses file:///C:/workfolder/jsPDF/examples/basic.html or http://localhost/jsPDF/examples/basic.html

Can somebody tell me what is the problem?

Thanks

Obsolete

Most helpful comment

This is caused by the following buggy code in jspdf.debug.js (lines 777-797):
(called by setFont)

if (fontName !== undefined){
    fontName = fontName.toLowerCase();
}
switch(fontName){
    case 'sans-serif':
    case 'verdana':
    case 'arial':
        fontName = 'helvetica';
        break;
    case 'fixed':
    case 'monospace':
    case 'terminal':
        fontName = 'courier';
        break;
    case 'serif':
    case 'cursive':
    case 'fantasy':
    default:
        fontName = 'times';
        break;
}

Instead of
helvetica --> arial

instead of
courier --> terminal

or remove that lines from your js file. (For example if you want to use your own font with addFont)

All 9 comments

jspdf.debug.js, version 1.1.135 does not work...

jspdf.debug.js, version 1.0.272 works!

I have the same issue.

This is caused by the following buggy code in jspdf.debug.js (lines 777-797):
(called by setFont)

if (fontName !== undefined){
    fontName = fontName.toLowerCase();
}
switch(fontName){
    case 'sans-serif':
    case 'verdana':
    case 'arial':
        fontName = 'helvetica';
        break;
    case 'fixed':
    case 'monospace':
    case 'terminal':
        fontName = 'courier';
        break;
    case 'serif':
    case 'cursive':
    case 'fantasy':
    default:
        fontName = 'times';
        break;
}

Instead of
helvetica --> arial

instead of
courier --> terminal

or remove that lines from your js file. (For example if you want to use your own font with addFont)

+1 for fixing this

Thanks @Eusebius1920 !
Your solution was the only one that worked in my case to use a custom font. Thanks

Hi, I am unable to use font Verdana to generate a pdf file. It gives me this warning: 'The Verdana font contains a bad BBox'. Did any of you guys sort this out ?

This are the steps that I've taken:

  1. create pdf doc
    var pdf = new jsPDF('p', 'in', 'letter'),
    pdf.addFont('verdana', 'verdana', 'normal');
    lines = pdf.setFont('verdana', 'normal')
    .setFontSize(12)
    .splitTextToSize(text, 10);
  2. commented out the lines of code which set the font default to helvetica
    lines: 805-827 from jspdf.debug.js

if (fontName !== undefined) { fontName = fontName.toLowerCase(); } switch (fontName) { case 'sans-serif': case 'verdana': case 'arial': case 'helvetica': fontName = 'helvetica'; break; case 'fixed': case 'monospace': case 'terminal': case 'courier': fontName = 'courier'; break; case 'serif': case 'cursive': case 'fantasy': default: fontName = 'times'; break; }
This is the version I'm using : Version 1.2.60-git Built on 2016-02-29T18:46
Any help is greatly appreciated.
Thank you.

@LeoAbu-Saa did you find any solution?

@havenchyk
Unfortunately not :( Stick to the default fonts.

replace "Times-Roman" with "Verdana" in JsPdf.debug.js file.... it will set default Verdana in jsPDF file

Was this page helpful?
0 / 5 - 0 ratings