jsPDF icon indicating copy to clipboard operation
jsPDF copied to clipboard

setFont does not work on local server

Open zedmartins opened this issue 11 years ago • 9 comments

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

zedmartins avatar May 22 '15 18:05 zedmartins

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

jspdf.debug.js, version 1.0.272 works!

zedmartins avatar May 22 '15 21:05 zedmartins

I have the same issue.

DavidePastore avatar May 25 '15 14:05 DavidePastore

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)

Eusebius1920 avatar Jun 03 '15 16:06 Eusebius1920

+1 for fixing this

tholu avatar Aug 31 '15 14:08 tholu

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

jonanderdev avatar Oct 21 '15 07:10 jonanderdev

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 avatar Mar 21 '16 10:03 LeoAbu-Saa

@LeoAbu-Saa did you find any solution?

havenchyk avatar Jun 30 '16 10:06 havenchyk

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

LeoAbu-Saa avatar Jun 30 '16 12:06 LeoAbu-Saa

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

pramod7777 avatar May 23 '17 11:05 pramod7777