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");
Related
Pptxgenjs is able to add SVG images and generate PPT correctly. When this PPT is opened using Microsoft office, SVG images are shown properly. However, when this PPT is opened using libreOffice, SVG images are shown as invalid cross symbols as mentioned below.
ENV: Windows 10
LibreOffice Version 7
Am I missing something here?
You can try using Aspose.Slides Cloud SDK for Node.js to add SVG images and other content to your presentations. You can evaluate this REST-based API making 150 free API calls per month for API learning and presentation processing. The following code example shows you how to add an SVG image to a presentation using Aspose.Slides Cloud:
const cloud = require("asposeslidescloud")
const model = require("asposeslidescloud/model")
const fs = require("fs")
const slidesApi = new cloud.SlidesApi("client_id", "client_key")
const fileName = "example.pptx"
const imagePath = "image.svg"
const slideIndex = 1
// Create a picture frame for the SVG image with a specified size and position.
var pictureFrame = new model.PictureFrame()
pictureFrame.pictureFillFormat = new model.PictureFill()
pictureFrame.pictureFillFormat.svgData = fs.readFileSync(imagePath, "utf8")
pictureFrame.X = 20
pictureFrame.Y = 20
pictureFrame.Width = 300
pictureFrame.Height = 200
// Add the picture frame on the slide.
slidesApi.createShape(fileName, slideIndex, pictureFrame).then((response) => {
console.log(JSON.stringify(response))
})
I work as a Support Developer at Aspose.
I have a file (for example JPEG size 1588 × 2244px). It's generated by puppeteer (but phantomjs also generate 72 DPI screenshot). When I save this image into a file with .jpeg extension and I use macOS General Info I see:
As you can see it has 72 DPI set in metadata, but I want to use file with 300 DPI. I know, that in digital it doesn't change anything - it's property for printing, but I don't want to explain to each customer that this file could be print in 300 DPI.
When I use Gimp and Image > Print Size
I can change the DPI, and export a picture again. And now it has 300 DPI in General Info window.
I try to do it in Node.JS server, but I found few options to change this property on .PNG pictures, but anyone is working for .JPEG files.
I think that the most accurate option is to use method changeDpiDataUrl from this library:
https://github.com/shutterstock/changeDPI/blob/master/src/index.js
But when I put my image as base64image, after a split operation I have the array with 1 element only - I think that this is body, so I don't have format property (at 63 line).
Anybody meet this problem before?
You can use the library piexifjs to change the EXIF data of an image. The library only changes the meta data (called EXIF), not the image itself.
Code Sample
The following code uses the API to read the EXIF data of an image, change it and create a new buffer from the changed data.
const piexif = require("piexifjs");
// get the image buffer from puppeteer or from disk
const imageBuffer = /* ... */
// convert buffer to string and load it
const imageString = imageBuffer.toString('binary');
const exif = piexif.load(imageString);
// change resolution
exif['0th'][piexif.ImageIFD.XResolution] = [300,1];
exif['0th'][piexif.ImageIFD.YResolution] = [300,1];
// generate new EXIF data
const newExifDump = piexif.dump(exif);
// generate new image
const newData = piexif.insert(newExifDump, imageString);
const jpgBuffer = new Buffer(newData, "binary");
// write to file or use buffer
// ...
Be aware, that I have not used the library myself, nor have I tested the code.
This solution is another alternative to the answer given by #thomasdondorf.
The resolution/density can be set easily using JavaScript standard image processing library sharp by withMetadata function.
Simple example:
// Set output metadata to 96 DPI
const data = await sharp(input)
.withMetadata({ density: 96 })
.toBuffer();
Npm module: sharp
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";
I want to make bold some part of a big string. I am using Xamarin iOS and getting a big string from API. I need to make some part of the string as bold I tried <b>some text </b> this will not work for mono touch. What is the best option to make a string bold in run time in xamrin. iOS or in winforms applications?
Use AttributedString to apply attributes to different parts of the string. See example below that will only make the first half of the string bold
var BoldString = new NSMutableAttributedString (original_string);
var BoldTextAttributes = new UIStringAttributes {
Font = UIFont.FromName ("Helvetica-Bold", 20f)
};
BoldString.SetAttributes (BoldTextAttributes.Dictionary, new NSRange (0, OutOfStock.Length / 2)); // this range will apply the attribute only to the first half of the string
MyLabel.AttributedText = BoldString; // Assign the attributed text to your label
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('武大郎')