I'm doing my nodejs + expressjs + mongodb project, I need fetch data from mongodb and then write it to pdf file, then send out by expressjs. everything seems fine except that the data is Japanese letter, and the encoding messed-up. I'm using pdfkit for creating pdf file, like this:
var doc = new PDFDocument();
doc.info['Title'] = profile.firstName + " " + profile.lastName;
doc.fillColor('black')
.text(profile.firstName + " " + profile.lastName, {
paragraphGap: 10,
indent: 20,
align: 'justify',
columns: 2
});
then the meta-info of the file and the only line of the content shows: "kf Y’˛" which is should be : "武 大郎"
so, is there any way to set the encoding in pdfkit? or some work around?
PDFKit supports embedding font files in the TrueType (.ttf), TrueType Collection (.ttc), and Datafork TrueType (.dfont) formats. (source: http://pdfkit.org/docs/text.html#fonts)
Download a Japanese Font in TrueType (.ttf) format here http://www.freejapanesefont.com/ipaex-gothic/
# Using a TrueType font (.ttf)
doc.font('fonts/ipaexg.ttf').text('武大郎')
Related
I recently used pdfbox android library because iText is under AGPL. I tried running following code.
PDDocument document = new PDDocument();
PDPage page= new PDPage();
document.addPage(page);
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
Bitmap bitmap=BitmapFactory.decodeFile(imagesObject.get(0).image); //imagesobject is string path
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
PDImageXObject pdImage = PDImageXObject.createFromFile(imagesObject.get(0).image,document);
PDPageContentStream contentStream = new PDPageContentStream(document, page,true,true);
contentStream.drawImage(pdImage,70,70,pdImage.getWidth(), pdImage.getHeight());
contentStream.close();
document.save(file);
document.close();
PDF is saved with empty page no image is shown. I noticed size of pdf is 6mb, Which means the image has been drawn but can't see. Any Fix?
Also I am using ported library by TomRoush.
This is the link for pdf that was generated here
As discussed in the comments, the image had a .jpg extension in the name, but was a PNG image file. The PDImageXObject createFromFile(String imagePath, PDDocument doc) method assumes the file type by its extension, so it embedded the file 1:1 in the PDF and assigned a DCT filter. Both of these would have been correct for a jpeg file, but not for png.
So the solution would be to either rename the file, or use the createFromFileByContent method.
I'm trying to overlay text on top of a GIF using the gm package. It does overlay the text on top of regular images, however on GIFs it just overlays the last 2 frames. I don't have much experience using this package and Imagemagick.
Here's the code that edits the image.
gm(request(image)) //any image url
.command('convert')
.coalesce()
.font("./impact.ttf")
.dither(false)
.fontSize(Math.round(width/11)) //Width is defined earlier in the code
.stroke("#000000")
.strokeWidth(width/450)
.out('-gravity', 'north')
.out('-size',width+'x','-fill','white','-background', 'transparent', '0')
.out('caption:' + meme[0].toUpperCase().substr(meme[0].indexOf(" ")+1))
.out('-layers','optimize')
.out('-gravity', 'south')
.out('-size',width+'x','caption:' + meme[1].toUpperCase())
.out('-layers','optimize')
.strip()
// If i use these two, it does append all the layers but the text does not wrap like it does using caption
// .drawText(0,1, meme[0].toUpperCase().substr(meme[0].indexOf(" ") + 1), 'North')
// .drawText(0,0, meme[1].toUpperCase(), 'South')
.stream((error, stdout) => {...}
Result of the code
Any help would be appreciated!
The user must upload ttf file,and I want to apply that font file for text,that's why I use base64 format in font-face,but something went wrong,and don't changed text font
https://jsfiddle.net/Meline222/xmeuqwp4/1/
var output = document.getElementById('output');
var f = new FontFace("fingerpaint", `url(data:font/truetype;charset=utf-8;base64,${ret})`);
console.log(dataURL)
f.load().then(function(loaded_face) {
output.style.fontFamily = "fingerpaint";
My code write Text to file in Adobe Script (.jsx):
var xml= " 小塚ゴシック Pro"
var file = new File(output);
file.open("w");
file.write(xml);
file.close();
But result encode UTF-8 can't display: ϬӋēĖĢĎ Pro
It only can display text " 小塚ゴシック Pro" , if set encode of file is Shift-JIS.
How write text to File by Encode UTF-8?
Try this:
file.encoding = "UTF-8";
And here is a sample, for reference.
i'm using pdfkit with nodejs to generate dynamically PDF files. the generation works fine but i have a problem displaying arabic characters even if i setup a font that support arabic.
The letters are rendered correctly, but the words are displayed character by character :(
here's my code
doc = new PDFDocument;
doc.pipe(fs.createWriteStream('output.pdf'));
var str = "فصل الربيع الزهور \n#nature #payesage #fleurs #plantes #vert #espace #temara #rabat #maroc #WeekEnd #balade #instamoment #instalife #instamaroc #photographie #macro #peace";
doc.font('resources/HelveticaNeueLTArabic-Roman.ttf').text(str);
Any thoughts or suggestions will be great.
Use Amiri font , it supports arabic font
const customFontRegular = fs.readFileSync(`./amiri/Amiri-Regular.ttf`);
const customFontBold = fs.readFileSync(`./amiri/Amiri-Bold.ttf`);
pdfDoc.registerFont(`Amiri-Regular`, customFontRegular);
pdfDoc.registerFont(`Amiri-Bold`, customFontBold);
And it can be used as
pdfDoc.font('Amiri-Regular').text("Hello world");