在Node.js中使用条形码生成pdf [英] Generate pdf with barcode in Node.js
问题描述
我正在使用
我找到了可以生成的库
看起来不错,但看起来与普通的UPC-A条形码并不完全一样.
我在 http://www.fontpalace.com/font使用了字体-download/UPC-A/.
我使用JSBarcode和Canvas在pdf中生成条形码.
您需要为此安装JSBarcode:
npm install jsbarcode
在PDF中生成条形码的代码:
var canvas = document.createElement("canvas");JsBarcode(画布,"3000001",{格式:"ean8",高度:20,displayValue:否});doc.image(canvas.toDataURL(),10,10,height:30,width:130);
I am using https://github.com/devongovett/pdfkit to generate PDF files which I can do simply with something like
app.get('/get-pdf', (req, res) => {
const doc = new PDFDocument();
const filename = 'my_pdf.pdf';
res.setHeader('Content-disposition', 'attachment; filename="' + filename + '"');
res.setHeader('Content-type', 'application/pdf');
const content = "Some content";
doc.y = 300;
doc.text(content, 50, 50);
doc.pipe(res);
doc.end();
});
But I also want to generate an UPC-A barcode:
I have found the library https://github.com/lindell/JsBarcode which can generate such barcode from just the 12-digit code. However, it seems the library is mainly used in the client.
I want to generate a PDF with such barcode, but I don't know how to do it or if JsBarcode isn't too complex for just this single type of barcode.
Edit
As suggested in the comments, I did try to generate a the barcode with the UPC-A font:
app.get('/get-pdf', (req, res) => {
const doc = new PDFDocument();
const filename = 'my_pdf.pdf';
res.setHeader('Content-disposition', 'attachment; filename="' + filename + '"');
res.setHeader('Content-type', 'application/pdf');
doc.font('/fonts/UPC-A.ttf').fontSize(50).text('012345678905');
doc.pipe(res);
doc.end();
});
from which I get
which does look good, but it doesn't look exactly like common UPC-A barcodes.
I used the font at http://www.fontpalace.com/font-download/UPC-A/.
I used JSBarcode and Canvas to generate Barcode within pdf.
You need to install JSBarcode for this:
npm install jsbarcode
Code to generate barcode inside PDF:
var canvas = document.createElement("canvas");
JsBarcode(canvas, "3000001", {
format: "ean8", height: 20,
displayValue: false
});
doc.image(canvas.toDataURL(), 10, 10, height:30, width:130);
这篇关于在Node.js中使用条形码生成pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!