Does pdfkit support Hindi language? I am writing a program to generate pdf document with Hindi text
Code sample
let user = {
no: 1,
name: 'अर्जुन दाव',
age: 26,
gender: 'पुरुष'
};
console.log(user);
const doc = new PDFDocument();
doc.fontSize(36)
.fillColor('red')
.text(user.name, 50, 400);
doc.fontSize(16)
.fillColor('black')
.text(`उम्र : ${user.age}, ${user.gender}`, 50, 460);
doc.pipe(fs.createWriteStream(`./output/${user.no}.pdf`));
doc.end();
Output
It prints the user object in the console as expected. But the pdf generated with shows a bunch of weird characters in place of texts.
environment
pdfkit version: ^0.11.0
Node version: v10.19.0
Operating System: ubuntu 20.0.4
What am I doing wrong?
Download https://fonts.google.com/specimen/Tiro+Devanagari+Hindi?query=hindi
and add in your code:
doc.font('./fonts/Tiro_Devanagari_Hindi/TiroDevanagariHindi-Regular.ttf');
That worked for me with Hindi input text without converting it to English or using transliteration.
Related
I have data that is being sent from the server to the frontend:
"data": {
"filename": "",
"content": {
"type": "Buffer",
"data": [
37,
80,
68,
70,
45,
49,
46,
52,
10,
37,
...,
This is the object I create in the backend using the md-to-pdf library. Since I do not want any temporary space I do not set a destination on the md-to-pdf function, instead I just save the pdf object into a variable and send it back to the frontend and I get what you can see above.
To create the pdf object I just pass in a string which contains a lot of markdown data through the content parameter. This works perfectly when I set a destination, I am able to create a pdf file with the markdown, but when I send the data of the pdf to the frontend..
I believe what I get is the decimal representation of a pdf file. What I am unsure of is how I can use this data on the frontend to create a Blob or something similar?
I have tried sending the data.content to a blob but all I get is a "failed to load the document" error. This is my blob which other than not specifying type, I believe is right.
startDownload = (pdf) => {
const element = document.createElement("a");
const blob = new Blob([pdf]);
element.href = URL.createObjectURL(blob);
element.download = "userTable.pdf";
element.click();
};
One thing I would like to point out is that if I change the element.download = "userTable.pdf" to element.download = "userTable.md" and open that on a browser I do not get error but the only thing I am able to see is the text [object object] and a black screen.
I am using the pdf-to-excel npm module for the node project to convert PDF files to XLSX spreadsheets. I have used the below syntax:
try {
const options = {
// when current pdf page number changes call this function(optional)
onProcess: (e) => console.warn(`${e.numPage} / ${e.numPages}`),
// pdf start page number you want to convert (optional, default 1)
start: 1,
// pdf end page number you want to convert (optional, default )
end: 2,
}
// bar.pdf in your static file server's root dir.
pdf2excel.genXlsx(filePath, `${outFilePath}output.xlsx`, options);
} catch (err) {
console.error(err);
}
But the problem which I'm facing is PDF has three pages with tabular content but they are not getting converted properly. The first page is appended as spreadsheet headings and the remaining pages as content or vice versa. How can I convert three pdf pages into three individual tabs in a single spreadsheet?
i'm trying to use fabric in my nodejs backend which is deployed in an alpine image ( node version 8.15 )
so i've made a function responsible for creating the template as the following
const creatCardTemplate = function(user){
let canvas = new fabric.Canvas(null, {width: 650, height: 400});
canvas.backgroundColor="#fff";
let company=user.company?user.company.companyName:"YOUR COMPANY";
let text = new fabric.IText(company,{fontSize:30,top:100,textAlign:'center',fontWeight:'bold'});
canvas.add(text);
canvas.centerObjectH(text);
text = new fabric.IText(user.first_name+" "+user.last_name,{fontSize:26,top:140,textAlign:'center',fontWeight:'bold'});
canvas.add(text);
canvas.centerObjectH(text);
:::: etc
canvas.renderAll();
return canvas;
}
a quite normal code and then i just generate a PNG image from it
let canvas = creatCardTemplate(user);
let image = canvas.toDataURL({ format: 'png'});
that's all,it works fine in my local machine but my problem is that when i deploy it the png result is so gebbrich like this
i tried many solutions like changing the creation method ( i tried to create the canvas by loading a json data ) also i tried to change the font family to Arial but always the same damn squares
After a lot of struggling i found the issue, the issue was that alpine image came without any font installed ( as we know that Fabric will use the browser fonts if it was installed in a front end framework) but in my case it was installed with nodejs (backend) so it will use the fonts available in the system.
So i've installed some fonts and added them to be used by Fabric as the following:
first of all we need to import
const { registerFont, createCanvas } = require('canvas')
then we add our desired font
registerFont('helpers/fonts/Roboto-Regular.ttf', { family: 'Roboto' })
then we have just to use it :)
let text = new fabric.IText("hello",{fontSize:30,top:100,textAlign:'center',fontFamily:'Roboto
What's the best way to transform the XML to a specific XML Format or JSON Format ?
Which npm packages can be used ?
I'd give xml2js a try, this makes XML to JSON conversion very easy:
const xml2js = require('xml2js');
function testXmlParse(xml) {
const parser = new xml2js.Parser( { explicitArray: false })
parser.parseString(xml, (err, result) => {
if (err) {
console.error("An error occurred: ", err);
} else {
console.log("Xml to Json result: ", result);
}
})
}
const xmlInput = "<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>";
testXmlParse(xmlInput);
Here's an example of some Xml manipulation using xmldom:
const xmldom = require('xmldom');
const xpath = require('xpath');
var parser = new xmldom.DOMParser();
var serializer = new xmldom.XMLSerializer();
const xml = '<catalog><book id="bk101"><author>Gambardella, Matthew</author><title>XML Developer\'s Guide</title><genre>Computer</genre><price>44.95</price><publish_date>2000-10-01</publish_date><description>An in-depth look at creating applicationswith XML.</description></book><book id="bk102"><author>Ralls, Kim</author><title>Midnight Rain</title><genre>Fantasy</genre><price>5.95</price><publish_date>2000-12-16</publish_date><description>A former architect battles corporate zombies,an evil sorceress, and her own childhood to become queenof the world.</description></book><book id="bk103"><author>Corets, Eva</author><title>Maeve Ascendant</title><genre>Fantasy</genre><price>5.95</price><publish_date>2000-11-17</publish_date><description>After the collapse of a nanotechnologysociety in England, the young survivors lay thefoundation for a new society.</description></book><book id="bk104"><author>Corets, Eva</author><title>Oberon\'s Legacy</title><genre>Fantasy</genre><price>5.95</price><publish_date>2001-03-10</publish_date><description>In post-apocalypse England, the mysteriousagent known only as Oberon helps to create a new lifefor the inhabitants of London. Sequel to MaeveAscendant.</description></book><book id="bk105"><author>Corets, Eva</author><title>The Sundered Grail</title><genre>Fantasy</genre><price>5.95</price><publish_date>2001-09-10</publish_date><description>The two daughters of Maeve, half-sisters,battle one another for control of England. Sequel toOberon\'s Legacy.</description></book><book id="bk106"><author>Randall, Cynthia</author><title>Lover Birds</title><genre>Romance</genre><price>4.95</price><publish_date>2000-09-02</publish_date><description>When Carla meets Paul at an ornithologyconference, tempers fly as feathers get ruffled.</description></book><book id="bk107"><author>Thurman, Paula</author><title>Splish Splash</title><genre>Romance</genre><price>4.95</price><publish_date>2000-11-02</publish_date><description>A deep sea diver finds true love twentythousand leagues beneath the sea.</description></book><book id="bk108"><author>Knorr, Stefan</author><title>Creepy Crawlies</title><genre>Horror</genre><price>4.95</price><publish_date>2000-12-06</publish_date><description>An anthology of horror stories about roaches,centipedes, scorpionsand other insects.</description></book><book id="bk109"><author>Kress, Peter</author><title>Paradox Lost</title><genre>Science Fiction</genre><price>6.95</price><publish_date>2000-11-02</publish_date><description>After an inadvertant trip through a HeisenbergUncertainty Device, James Salway discovers the problemsof being quantum.</description></book><book id="bk110"><author>O\'Brien, Tim</author><title>Microsoft .NET: The Programming Bible</title><genre>Computer</genre><price>36.95</price><publish_date>2000-12-09</publish_date><description>Microsoft\'s .NET initiative is explored indetail in this deep programmer\'s reference.</description></book><book id="bk111"><author>O\'Brien, Tim</author><title>MSXML3: A Comprehensive Guide</title><genre>Computer</genre><price>36.95</price><publish_date>2000-12-01</publish_date><description>The Microsoft MSXML3 parser is covered indetail, with attention to XML DOM interfaces, XSLT processing,SAX and more.</description></book><book id="bk112"><author>Galos, Mike</author><title>Visual Studio 7: A Comprehensive Guide</title><genre>Computer</genre><price>49.95</price><publish_date>2001-04-16</publish_date><description>Microsoft Visual Studio 7 is explored in depth,looking at how Visual Basic, Visual C++, C#, and ASP+ areintegrated into a comprehensive developmentenvironment.</description></book></catalog>';
const root = parser.parseFromString(xml);
let selectedNodes = xpath.select('//book/author', root);
var newDoc = new xmldom.DOMParser().parseFromString("<test/>");
// Move nodes to new document..
selectedNodes.forEach(function (n) {
newDoc.appendChild(n);
});
console.log("Processed Xml: ", serializer.serializeToString(newDoc));
I build camaro for that specific purpose, to extract and transform the xml input. You can see the below image to get an idea of how it works
basically, you write a xpath template specify what field you want, where is it (via xpath), how do you want to name it in json output
I have a custom Active Reports server implemented in an httphandler, which can generate and export reports in the various formats available using the AR run-time ( excel, html, PDF, ... ). I'm now trying to use the JavaScript HTML5 viewer but it doesn't seem to be compatible with with any of the obvious export formats. The documentation and examples all show using the HTML5 viewer with the actual Active Reports server product and there are no examples for using it with a custom report service.
Client Code:
var viewer = GrapeCity.ActiveReports.Viewer(
{
element: '#reportViewer',
report: {
id: "report1.rdlx"
},
reportService: {
url: /MyCustomReportService/reports.mrs?msg={....}
},
uiType: 'desktop'
});
Server Code:
request.context.Response.ContentType = "image/gif";
MemoryStream memoryStream = new MemoryStream();
report.Document.Save(memoryStream);
context.Response.BinaryWrite(memoryStream.ToArray());
also tried
request.context.Response.ContentType = "message/rfc822";
HtmlExport html = new HtmlExport();
html.OutputType = HtmlOutputType.DynamicHtml;
MemoryStream memoryStream = new MemoryStream();
html.Export(report.Document, memoryStream);
request.context.Response.BinaryWrite(memoryStream.ToArray());
From what I understand, you have two requirements :
(Please refer to my forum posts for each of the below requirements)
>>>> Using the HTML5Viewer with a CustomReportService
http://arhelp.grapecity.com/groups/topic/rdlx-and-web-config/#post-658924
http://arhelp.grapecity.com/groups/topic/crystal-report-object-data-source-html5/#post-660665
>>>> Implementing Custom Export in HTML5Viewer
http://arhelp.grapecity.com/groups/topic/csv-export-from-html5-viewer/#post-658376
http://arhelp.grapecity.com/groups/topic/html5-viewer-export-to-xlsx/#post-657866
http://arhelp.grapecity.com/groups/topic/export-to-rtf-using-html5-viewer/#post-656001
Let me know if that does not works for you or you have any further issues.
Regards,
Reema