2016-05-05 131 views
1

我需要與中國信導出PDF,並編寫代碼如下鏈接說:添加自定義字體宋體在jsPDF

Custom font faces in jsPDF?

和我的代碼:

CSS:

@font-face { 
    font-family: SimSun; 
    font-weight: normal; 
    font-style: normal; 
    src: url("./simsun.ttc"); 
} 

body{ 
    font-family: 'SimSun'; 
} 

js:

pdf.addFont('SimSun', 'SimSun', 'normal','StandardEncoding'); 
pdf.setFont('SimSun'); 
pdf.text(20, 20, '西溪園區 5號樓 1F'); 
pdf.save('TestSVG.pdf'); 

正如有人說的,我已經證實addFont是公開的:

API.addFont = function(postScriptName, fontName, fontStyle) { 
    addFont(postScriptName, fontName, fontStyle, 'StandardEncoding'); 
}; 

而且我還發現「中Simsun」的postScriptName是宋體。 http://mirror.sars.tw/FreeBSD_Chinese_HOWTO/simsun.html

有人可以告訴我爲什麼嗎?

回答

0

你嘗試加載一個TrueType 收集,不是一個TrueType字體。當前版本的CSS不支持字體集合,因此請解壓縮您的集合,然後加載單個字體。

@font-face { 
    font-family: SimSun; 
    font-weight: normal; 
    font-style: normal; 
    src: url("./simsunregular.ttf"); 
} 

@font-face { 
    font-family: SimSun; 
    font-weight: bold; 
    font-style: normal; 
    src: url("./simsunbold.ttf"); 
} 

...etc... 

body{ 
    font-family: 'SimSun'; 
} 
+0

這會影響html中的單詞,但不能影響pdf中的單詞。當我導出pdf文件並用chrome打開時,它是亂碼。 –

+0

那麼我建議首先問你應該問的第一個地方:https://github.com/MrRio/jsPDF/issues –