galkahana / HummusJS

Node.js module for high performance creation, modification and parsing of PDF files and streams
http://www.pdfhummus.com
Other
1.15k stars 170 forks source link

How to write Korean characters? #204

Open rifi opened 7 years ago

rifi commented 7 years ago

Hi.. I don't know how to write korean character and how to use korean font. I am beginner in pdf programming. So I read ocumentation and issues. But I don't understand..

My Test text is 'Hello World 한글 abc!'. Korean text part '한글' not display

  1. Can I use korean characters? If possible, how to use? Git include test font not display or blank box or etc ....

  2. I tried NotoSansCJKkr(otf) - https://www.google.com/get/noto/#serif-kore How can I use this font? If possible, how?

    My test code

    var contentContext = pdfWriter.startPageContentContext(page); [pdfWriter.getFontForFile(__dirname + '/TestMaterials/fonts/NotoSansCJKkr-Regular.otf',0)
    ].forEach(function(element,index,array) { contentContext .BT() .k(0,0,0,1) .Tf(element,1) .Tm(30,0,0,30,78.4252,662.8997 - index*100) .Tj('Hello World 한글 abc!') .ET(); });

Thank u for reading... Help me

galkahana commented 7 years ago

Could be because the font does not support unicode mapping. Try looking for a unicode korean font (googling should suffice, would put somethibg myself but i dont know what you like), and use instead. If this will solve the problem, theb we know why, then we can discuss how to work with a font thats not unicode.

Regardless ill try to find some time soon to look into this. Gal

DamickDoni commented 7 years ago

Instead of using Tj use writeText and add a font that can handle Korean Glyphs.

galkahana commented 7 years ago

Dear sir, I was looking at the issue at hand, and found that when using NotoSansCJKkr-Regular.otf there's a bug that's awoken in the library. on my PC it was causing an endless loop. perhaps it was causing another effect on your side. fixing the bug, shows the characters as expected. Note that per @DamickDoni note, using writeText is a simpler version than running the BT-Tj-ET hupla, while being quite equivalent in all terms.

this sample program shows how, and i checked it to work:

var hummus = require('hummus')
var pdfWriter = hummus.createWriter('./hello.pdf',{log:'hello.log'})

var page = pdfWriter.createPage(0,0,595,842)

var cxt = pdfWriter.startPageContentContext(page);

var textOptions = {
    font: pdfWriter.getFontForFile('./NotoSerifCJKkr-Regular.otf'),
    size: 14,
    colorspace: 'gray',
    color: 0x00
};

pdfWriter.startPageContentContext(page).writeText('Hello World 한글 abc!',10,300,textOptions)

pdfWriter.writePage(page);
pdfWriter.end();    

you should be using version 1.0.83 of hummus to get the correction.