Convert/normalize Special Characters When Using Jspdf
Solution 1:
We can read here:
jsPDF supports finally UTF-8 by having the ability to use custom fonts.
The problem which you have is that you do not really realize how the PDF works. It must have some font which can display correct letters. It must be one system font (for PDF reader) or embeded font. And for each one single letter the PDF have to have one correct font. In this case for each word in new language in the same PDF you have to set the correct font.
Some TTF fonts was created for some specific letters, but not all TTFs was correctly created because behind this is one standard technology. Also not all of TTF fonts which was created for some specific letters can display them in PDF. For example font "Devanagari" which I have found in internet schould support all hindi letters, but it has failed fully.
Also we have to find the correct TTF fonts. And I found them - in your case for the string "тАШтЖТKad─▒k├╢yтАЩ" you could use "Courier New" or "Arial Unicode MS".
I have searched for each letter from your task and have found following lists:
тЖТ тАУ Font support for "Rightwards arrow" (u+2192)
─▒ тАУ Font support for "Latin small letter dotless I" (u+0131)
тАШ тАУ Font support for "Left single quotation mark" (u+2018)
тАЩ тАУ Font support for "Right single quotation mark" (u+2019)
├╢ тАУ Font support for "Latin small letter o with diaeresis'" (u+00F6)
Solution for most languages of the world
I have created the application which can create PDFs for most of languages in the world.
How to use it:
- At first download and extract free TTF font "Arial Unicode MS"
- Start the snippet below and choose the extracted free TTF font "Arial Unicode MS" from your folder.
- Write the text in your language and click on "Create PDF" button.
- The PDF will be downloaded and you could open it.
In some cases it could be that your language is not supported in TTF font "Arial Unicode MS". The full list of supported languages you can find here. In this case you have to find one from the correct TTF font. But be careful: if the font is under 100 kb. I have the expirience that does not work with jsPDF (see the beginning of my post).
The application
var fontInBase64 = '',
fileName = '',
message = document.querySelector('div'),
txtForPdf = document.querySelector('textarea'),
errorStr = '<b style="color:red">Please select a font file!</b>';
function readFile()
{
var file = document.querySelector('input[type=file]').files[0],
reader = new FileReader();
if(file && file.name.split('.')[1].toLowerCase() != 'ttf')
{
message.innerHTML = errorStr;
return;
}
if(txtForPdf.value.replace(/\s+/g, '').length < 1)
{
message.innerHTML = '<b style="color:red">Please write some Text!</b>';;
return;
}
reader.onloadend = function()
{
fontInBase64 = reader.result.split(',')[1];
fileName = file.name.replace(/\s+/g, '-');
createPDF(fileName, fontInBase64);
}
if(file) reader.readAsDataURL(file);
else message.innerHTML = errorStr;
}
function createPDF(fileName, fontInBase64)
{
var doc = new jsPDF('p','mm','a4');
fileNameWithoutExtension = fileName.split('.')[0],
lMargin = 15, // left margin in mm
rMargin = 15, // right margin in mm
pdfInMM = 210; // width of A4 in mm
doc.addFileToVFS(fileName, fontInBase64);
doc.addFont(fileName, fileNameWithoutExtension, 'normal');
doc.setFont(fileNameWithoutExtension);
doc.setFontSize(14);
var splitParts = doc.splitTextToSize(txtForPdf.value, (pdfInMM - lMargin - rMargin));
doc.text(15, 15, splitParts);
doc.save('test.pdf');
}
function setHindiToTextArea()
{
txtForPdf.value =
"рд╣рд┐рдиреНрджреА рд╡рд┐рд╢реНрд╡ рдХреА рдПрдХ рдкреНрд░рдореБрдЦ рднрд╛рд╖рд╛ рд╣реИ рдПрд╡рдВ рднрд╛рд░рдд рдХреА рд░рд╛рдЬрднрд╛рд╖рд╛ рд╣реИред рдХреЗрдВрджреНрд░реАрдп рд╕реНрддрд░ рдкрд░ рднрд╛рд░рдд рдореЗрдВ рджреВрд╕рд░реА рдЖрдзрд┐рдХрд╛рд░рд┐рдХ рднрд╛рд╖рд╛ рдЕрдВрдЧреНрд░реЗрдЬреА рд╣реИред рдпрд╣ рд╣рд┐рдиреНрджреБрд╕реНрддрд╛рдиреА рднрд╛рд╖рд╛ рдХреА рдПрдХ рдорд╛рдирдХреАрдХреГрдд рд░реВрдк рд╣реИ рдЬрд┐рд╕рдореЗрдВ рд╕рдВрд╕реНрдХреГрдд рдХреЗ рддрддреНрд╕рдо рддрдерд╛ рддрджреНрднрд╡ рд╢рдмреНрдж рдХрд╛ рдкреНрд░рдпреЛрдЧ рдЕрдзрд┐рдХ рд╣реИрдВ рдФрд░ рдЕрд░рдмреА-рдлрд╝рд╛рд░рд╕реА рд╢рдмреНрдж рдХрдо рд╣реИрдВред рд╣рд┐рдиреНрджреА рд╕рдВрд╡реИрдзрд╛рдирд┐рдХ рд░реВрдк рд╕реЗ рднрд╛рд░рдд рдХреА рдкреНрд░рдердо рд░рд╛рдЬрднрд╛рд╖рд╛ рдФрд░ рднрд╛рд░рдд рдХреА рд╕рдмрд╕реЗ рдЕрдзрд┐рдХ рдмреЛрд▓реА рдФрд░ рд╕рдордЭреА рдЬрд╛рдиреЗ рд╡рд╛рд▓реА рднрд╛рд╖рд╛ рд╣реИред рд╣рд╛рд▓рд╛рдВрдХрд┐, рд╣рд┐рдиреНрджреА рднрд╛рд░рдд рдХреА рд░рд╛рд╖реНрдЯреНрд░рднрд╛рд╖рд╛ рдирд╣реАрдВ рд╣реИ рдХреНрдпреЛрдВрдХрд┐ рднрд╛рд░рдд рдХрд╛ рд╕рдВрд╡рд┐рдзрд╛рди рдореЗрдВ рдХреЛрдИ рднреА рднрд╛рд╖рд╛ рдХреЛ рдРрд╕рд╛ рджрд░реНрдЬрд╛ рдирд╣реАрдВ рджрд┐рдпрд╛ рдЧрдпрд╛ рдерд╛ред рдЪреАрдиреА рдХреЗ рдмрд╛рдж рдпрд╣ рд╡рд┐рд╢реНрд╡ рдореЗрдВ рд╕рдмрд╕реЗ рдЕрдзрд┐рдХ рдмреЛрд▓реА рдЬрд╛рдиреЗ рд╡рд╛рд▓реА рднрд╛рд╖рд╛ рднреА рд╣реИред рд╡рд┐рд╢реНрд╡ рдЖрд░реНрдерд┐рдХ рдордВрдЪ рдХреА рдЧрдгрдирд╛ рдХреЗ рдЕрдиреБрд╕рд╛рд░ рдпрд╣ рд╡рд┐рд╢реНрд╡ рдХреА рджрд╕ рд╢рдХреНрддрд┐рд╢рд╛рд▓реА рднрд╛рд╖рд╛рдУрдВ рдореЗрдВ рд╕реЗ рдПрдХ рд╣реИред рд╣рд┐рдиреНрджреА рдФрд░ рдЗрд╕рдХреА рдмреЛрд▓рд┐рдпрд╛рдБ рд╕рдореНрдкреВрд░реНрдг рднрд╛рд░рдд рдХреЗ рд╡рд┐рд╡рд┐рдз рд░рд╛рдЬреНрдпреЛрдВ рдореЗрдВ рдмреЛрд▓реА рдЬрд╛рддреА рд╣реИрдВред рднрд╛рд░рдд рдФрд░ рдЕрдиреНрдп рджреЗрд╢реЛрдВ рдореЗрдВ рднреА рд▓реЛрдЧ рд╣рд┐рдиреНрджреА рдмреЛрд▓рддреЗ, рдкрдврд╝рддреЗ рдФрд░ рд▓рд┐рдЦрддреЗ рд╣реИрдВред рдлрд╝рд┐рдЬреА, рдореЙрд░рд┐рд╢рд╕, рдЧрдпрд╛рдирд╛, рд╕реВрд░реАрдирд╛рдо рдХреА рдФрд░ рдиреЗрдкрд╛рд▓ рдХреА рдЬрдирддрд╛ рднреА рд╣рд┐рдиреНрджреА рдмреЛрд▓рддреА рд╣реИред 2001 рдХреА рднрд╛рд░рддреАрдп рдЬрдирдЧрдгрдирд╛ рдореЗрдВ рднрд╛рд░рдд рдореЗрдВ рекреи рдХрд░реЛрдбрд╝ реиреж рд▓рд╛рдЦ рд▓реЛрдЧреЛрдВ рдиреЗ рд╣рд┐рдиреНрджреА рдХреЛ рдЕрдкрдиреА рдореВрд▓ рднрд╛рд╖рд╛ рдмрддрд╛рдпрд╛ред рднрд╛рд░рдд рдХреЗ рдмрд╛рд╣рд░, рд╣рд┐рдиреНрджреА рдмреЛрд▓рдиреЗ рд╡рд╛рд▓реЗ рд╕рдВрдпреБрдХреНрдд рд░рд╛рдЬреНрдп рдЕрдореЗрд░рд┐рдХрд╛ рдореЗрдВ 648,983; рдореЙрд░реАрд╢рд╕ рдореЗрдВ рем,реорел,резренреж; рджрдХреНрд╖рд┐рдг рдЕрдлреНрд░реАрдХрд╛ рдореЗрдВ рео,репреж,реирепреи; рдпрдорди рдореЗрдВ реи,рейреи,ренремреж; рдпреБрдЧрд╛рдВрдбрд╛ рдореЗрдВ рез,рекрен,режрежреж; рд╕рд┐рдВрдЧрд╛рдкреБрд░ рдореЗрдВ рел,режрежреж; рдиреЗрдкрд╛рд▓ рдореЗрдВ рео рд▓рд╛рдЦ; рдЬрд░реНрдордиреА рдореЗрдВ рейреж,режрежреж рд╣реИрдВред рдиреНрдпреВрдЬреАрд▓реИрдВрдб рдореЗрдВ рд╣рд┐рдиреНрджреА рдЪреМрдереА рд╕рд░реНрд╡рд╛рдзрд┐рдХ рдмреЛрд▓реА рдЬрд╛рдиреЗ рд╡рд╛рд▓реА рднрд╛рд╖рд╛ рд╣реИ";
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.4.1/jspdf.min.js" crossorigin="anonymous"></script>
<input type="file" onchange="message.innerHTML=' '"><br><br>
<textarea rows="4" cols="75">тАШтЖТKad─▒k├╢yтАЩ</textarea>
<div> </div>
<input type="button" value="Create PDF with UTF support" onclick="readFile()">
<br>
<i>For example</i>:<br><a href="#" onclick="setHindiToTextArea()"><b>Click on this line if you wont to set hindi text to the textarea.</b></a>
Solution 2:
You can make it with importing a font that supports your special characters.
From basic.js on examples you see reference how to apply it.
(Example brings cyrillic letters).
function demoUsingTTFFont() {
//https://fonts.google.com/specimen/PT+Sans
var PTSans = тАЬ...... тАЬ); // place long string of text here
var doc = new jsPDF();
doc.addFileToVFS("PTSans.ttf", PTSans);
doc.addFont('PTSans.ttf', 'PTSans', 'normal');
doc.setFont('PTSans'); // set font
doc.setFontSize(10);
doc.text("╨Р ╨╜╤Г ╤З╨╕╨║╨╕ ╨▒╤А╨╕╨║╨╕ ╨╕ ╨▓ ╨┤╨░╨╝╨║╨╕!", 10, 10);
doc.save('test.pdf');
}
As a fontfamily, please have a look to Google's Noto.
Source:
https://github.com/MrRio/jsPDF/issues/12 (scroll to down)
Solution 3:
imho, mico answer OK, only replace the font PTSans with the one you use (base64 encode). See jsfiddle: https://jsfiddle.net/o0m9pzyv/12/
var PTSans = ...

Post a Comment for "Convert/normalize Special Characters When Using Jspdf"