Reputation: 11039
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.
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/.
Upvotes: 7
Views: 14071
Reputation: 838
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);
Upvotes: 1
Reputation: 29012
Using the right font file (your UPC-A.ttf
in this case) is not enough, the numbers also need to be encoded correctly.
With this TTF file, the following code works (assuming that barcode
is already a 12-digit string including a correct check digit - if not, you can use something like the npm module cdigit
to calculate the check digit first):
const barcodeEncoded =
// First digit including leftmost bar
String.fromCharCode(Number(barcode[0]) + 0x50) +
// Regular digits in the left half
barcode.slice(1, 6) +
// Middle bar
'p' +
// Differently encoded digits in the right half
barcode.slice(6, 11).split('').map(x => String.fromCharCode(Number(x) + 0x40)).join('') +
// Last digit including rightmost bar
String.fromCharCode(Number(barcode[11]) + 0x60)
With this, 012345678905
turns into P12345pFGHI@e
which will then be a valid UPC-A barcode with this font.
For more information, check the readme of the font and this article about how such barcode is constructed.
Upvotes: 0
Reputation: 13669
Download ttf
font file for barcode from google font : https://fonts.google.com/?query=barcode (based on your requirements)
in my case i only want barcode without text , so im using https://fonts.google.com/specimen/Libre+Barcode+39
put this ttf font file in side public/fonts
folder
doc.font(__dirname+ "/../../public/fonts/LibreBarcode39-Regular.ttf").fontSize(20).text(012345678905);
it will generate below barcode :
if you want to use barcode with text then use this font : https://fonts.google.com/specimen/Libre+Barcode+39+Text
Upvotes: 2
Reputation: 2313
I never used JsBarcode
so I can't help you with this lib. But the logic is to get the Base64 String of the code bar and add it to your pdf like an image.
I use the npm module rescode
for doing that in my project. The code below generate a ean8 code bar and add this image in the PDF and it's work fine.
codes = require('rescode')
codes.loadModules(['ean8'])
dataEan8 = codes.create('ean8', '12345678')
doc.image(dataEan8, 10, 10, height:30, width:130)
Upvotes: 0