Adding barcodes to pdfs
is there any way to do something like a mail merge, where the data (9-15 chars long) is converted to a barcode? im using trying to use openoffice's code128 开发者_运维知识库for calc, but for some reason, every 10 strings, the barcode goes crazy, and the ascii tells me to register at the site where the extension came from, which i dont want to do
i also found one for oodraw, but that requires the values to be inputted manually. since im not familiar with the macros, i can't write something that will do it automatically
what im trying to do is:
take an old pdf (only 1 page)
covert it to word or picture or something
add a function/macro/whatever to show a barcode
(whether or not the barcode shows in this file, i dont care),
given a string from excel data
reconvert to separate pdfs
or some other way that adds barcodes to pdfs
all other free programs i have found do not do this nicely, and since im not really a pdf person, im not going to buy random programs. i just need this done for one large batch of data
I have came across a similar problem myself. Using python with reportlab and pyPdf, you can place a barcode (including string tag) into a template pdf. Here is a link to my gitlab repo with the python code:
https://github.com/glokem/barcodepdf
from pyPdf import PdfFileWriter, PdfFileReader
import StringIO
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import mm
from reportlab.graphics.barcode import code39
This block indicates all the required imports for python to do its work.
string = '0000000'
x_var=0
y_var=10
Just some minor housekeeping
packet = StringIO.StringIO()
slab = canvas.Canvas(packet, pagesize=A4)
slab.setFillColorRGB(0,0,0)
barcode = code39.Extended39(string,barWidth=.5*mm,barHeight=10*mm, checksum=0)
barcode.drawOn(slab, x_var*mm , y_var*mm)
slab.setFont("Courier", 25)
slab.drawString(40, 10, string)
slab.save()
This first block creates the barcode, places it on the 'slab' ready to be turned into a pdf. Note mm units, this may not suit those Imperial inclined.
packet.seek(0)
new_pdf = PdfFileReader(packet)
existing_pdf = PdfFileReader(file("template.pdf", "rb"))
output = PdfFileWriter()
page = existing_pdf.getPage(0)
page.mergePage(new_pdf.getPage(0))
output.addPage(page)
outputStream = file("destination.pdf", "wb")
output.write(outputStream)
outputStream.close()
This block is PDF creation magic which i dont fully understand.
With your 'template.pdf' already in place, things should work fine. This will place barcode for 00000000 in the bottom left corner.
Big thanks for the people behind reportlab, and pyPdf for providing an awesome platform here!
Edit: code added for clarity: thanks Amicable and Nico.
There's this handy dandy font called IDAutomationHC39M that's free to use - well there are limitations - but it writes the text in bar code format. Simple as that. No brainer. Barcode Font
精彩评论