i am using pdfkit for convert for create PDF file and it's work fine with this code.
var fs = require('fs');
var PDFDocument = require('pdfkit');
pdf.text('{{ }} text ');
pdf.pipe(
fs.createWriteStream('./data/file1.pdf')
)
.on('finish', function () {
console.log('PDF closed');
});
// Close PDF and write file.
pdf.end();
this code are work pretty but i want to convert chines text and some special character too with a-z text for example i want to make pdf of
pdf.text('{{ }} text 漢字 昨夜のコンサートは最高でした');
this is not give me proper output .
Unfortunately PDFKit doesn't seem to support converting documents to PDF:
The documentation states nothing about converting. This module seems to be purely for creating the documents from scratch.
You'll have to find a module or create something that takes an MS Doc and converts it to text, store this in your node app, then pass it through to PDFKit via the methods shown in the documentation.
Mammoth seems to do this.
There's a bunch of docx modules that might do an even better job.
Hope this helps!
Related
var imgStr= new Buffer.from(img).toString('base64');
I have been using this, but it is not working. This is the code to convert the screenshot i have taken into base64 encoding.
You don't need to use new
var imgStr = Buffer.from(imgBuffer).toString('base64');
I'm looking for an RTF to PDF converter for Node.js. I'm not finding too many packages over at the npm site.
The application is as follows: I need to pull an RTF document (created in Word and saved as .rtf) from an AWS S3 bucket. Then I need to read the file as an RTF string. Next, I need to replace certain placeholder tokens in the RTF string with values (ex. in "{host-name} would like to invite you to {address} on {date}."--I need to replace {host-name}, {address}, and {date} with actual values). Then, with actual values plugged in, I need to convert the string to a PDF that I can attach to an email.
For this reason, I cannot use any of the several Word-to-PDF packages out there (because I need to intervene in the intermediate step of plugging the placeholders with values), so I'm looking for an RTF-to-PDF converted (where the RTF starts as a string).
Is there such a thing?
Thanks.
LibreOffice in headless mode should provide file conversion to PDF.
You can integrate it in node with LibreOffice-Convert.
However you'll need LibreOffice in your machine, as it's not installed with npm. See here for the location according to your system.
This is how it's used in the command line, so it supports RTF to PDF conversion:
soffice --headless --convert-to pdf file_name.rtf
(source)
In theory it should be implemented like this (adapted from npmjs):
const libre = require('libreoffice-convert');
const path = require('path');
const fs = require('fs');
const enterPath = path.join(__dirname, '/resources/example.rtf');
const outputPath = path.join(__dirname, '/resources/example.pdf');
// Read file
const file = fs.readFileSync(enterPath);
// Convert it to pdf format with undefined filter (see Libreoffice doc about filter)
libre.convert(file, '.pdf', undefined, (err, done) => {
if (err) {
console.log(`Error converting file: ${err}`);
}
// Here in done you have pdf file which you can save or transfer in another stream
fs.writeFileSync(outputPath, done);
});
I am using node version 7.10.0 with:
pdf2json version 1.1.7
mail parser version 2.0.5
smtp-server version 3.0.1
I have received an email with a PDF attachment and I want to parse the PDF using pdf2json without saving the PDF to disk, I have the byte array from the email attachment. How can I parse this directly to the module?
I thought I could do this:
const pdfParser = require("pdf2json");
let objPDF = new pdfParser();
objPDF.parseBuffer(arybytContent);
Where 'arybytContent' is the content from the mail attachment:
arybytContent = objAttachment["content"]["data"];
Looking at objPDF using Chrome, it doesn't look like the content has been parsed correctly, is the content encoded in some way?
I've tried searching online for help, but couldn't find anything.
The solution in the end was to convert the array to a Buffer, attempting to write an array using available fs methods would result in a comma delimited set of values. A buffer is written as binary data.
I have a lambda script that retrieves an email from s3, parses it with MailParser (streaming), transforms attachments to csv if necessary, and stores them in a different bucket. The script handles csv files (no conversion) and zip files, but I can't figure out how to convert xls to csv using streams.
Exceljs looks really good for this, but I can't get it to work for some reason (and I'm really new to streams so that's probably it).
var workbook = new Excel.Workbook();
var xstream = workbook.xlsx.createInputStream();
xstream.on('done', function(data) {
// convert to csv and s3.upload
});
attachment.stream
.pipe(xstream);
I'm getting an error
Error: Unexpected xml node in parseOpen
before the 'done' event so I'm not sure if I'm using createInputStream correctly.
I'm creating a thumbnail from the first page of a PDF with the Node gm module.
var fs = require('fs');
var gm = require('gm');
var writeStream = fs.createWriteStream("cover.jpg");
// Create JPG from page 0 of the PDF
gm("file.pdf[0]").setFormat("jpg").write(writeStream, function(error){
if (!error) {
console.log("Finished saving JPG");
}
});
There's two problems with the script.
It creates a file cover.jpg, but that file is empty (size 0) and can't be opened by any viewer.
It creates a file named [object Object] that is an image of the PDF's first page (this is what I want, but the wrong name).
Aside from doing some additional file system manipulation to rename the [object Object] file after generating it, is there something I can change in the way I am using gm and fs in this script to write the image directly to the cover.jpg file?
This question is similar to what I am asking, but there is no accepted working answer and I need to install yet another library to use it (undesirable).
write receives the file path as the first argument, not a write stream, therefore the method is converting the stream object into its string representation, that's why it saves a file named [object Object].
You can just use .write("cover.jpg"), or if you want to use a write stream, you may use .stream().pipe(writeStream).
Take a look at the stream examples of gm.