How can I add barcode support to html2ps?
We are using the html2ps Perl script to convert HTML to Po开发者_Python百科stScript. I know it's not the best solution but this is what the developers here did (it was before my time).
Now we need to implement support for barcode fonts that is showing each HTML and needs to be shown in each converted PostScript file. How do you do this?
OK, I've got it working only with fonts (no images, as required). Here is how:
- Take your barcode font (I know nothing about barcodes, so I took the Code 128 freeware TrueType font; download link).
- Make it into a Postcript (type 1) font, with extension
.pfa
. I use fontforge to perform this conversion, like so: open the ttf font, choose File > Generate Fonts, with "PS Type 1 (ASCII), and dismiss possible warnings. - So now, I have my font "Code128bWin" in PS Type 1 format in a file named
Code128bWin.pfa
. - Create a minimalistic test document (
test.html
):
<html> <head></head> <body> <p>This is a test:</p> <h4>BARCODE</h4> </body> </html>
- Create a configuration file (this is what took time to understand; you cannot put it as inline CSS in the HTML; it needs to be a configuration file for html2ps). So, create a file named
config
:
H4 { font-family: Code128bWin } @html2ps { font { Code128bWin { names: "Code128bWin"; files: "Code128bWin.pfa"; } } }
- So, text in
<h4>
tags will be rendered with the codebar font. You now generate the Postscript output by running:html2ps -f config test.html > test.ps
Comments welcome!
As a sidenote: this is clearly ugly, but sometimes it's necessary to hack into an existing process, so I won't judge the OP. I think this solution satisfies all constraints :)
so what if seen in the comments is:
- you cannot use images created before, which are embedded in the html by using an
<img />
-tag - you cannot include additional fonts to create the barcode (does not work)
To #1: Is not not possible, because you cannot store images? If this is the reason, try using online code creators like barcodesinc, e.g.:
http://www.barcodesinc.com/generator/image.php?code=Hello World&style=197&type=C128B&width=200&height=50&xres=1&font=3
Example:
If you cannot rely on external service providers, there are free classes for your avail for free on the web, e.g. Barcode Generator. Deploy on a web server and use a tag like <img src="http://myserver/myscript.php?code=HelloWorld"/>
pointing to this script. html2ps is supporting rendering inline images.
There is also an open source postscript barcode writer available at http://www.terryburton.co.uk/barcodewriter/. After using html2ps you could manipulate the ps files and embed the barcodewriter directive, e.g.:
50 450 moveto (978-1-86074-271) (includetext) /isbn /uk.co.terryburton.bwipp findresource exec
To #2: Could you provide an error message? At which stage do you have problems?
Ehmm... #3: Using only html + css (would only use if all others fail)
Barcodes can be created using simple css and span-elements:
<style>
.barcode {
border:5px solid white;
background:white;
width:310px;
text-align:center;
}
.ns{
border-left:2px solid white;
height:30;
}
.nb{
border-left:2px solid black;
height:30;
}
.ws{
border-left:5px solid white;
height:30;
}
.wb{
border-left:5px solid black;
height:30;
}
</style>
<div class='barcode' id='bcx2_bc' ><span class='nb'></span><span class='ws'></span><span class='nb'></span><span class='ns'></span><span class='wb'></span><span class='ns'></span><span class='wb'></span><span class='ns'></span><span class='nb'></span><span class='ns'></span><span class='nb'></span><span class='ns'></span><span class='nb'></span><span class='ns'></span><span class='wb'></span><span class='ns'></span><span class='wb'></span><span class='ws'></span><span class='nb'></span><span class='ns'></span><span class='wb'></span><span class='ns'></span><span class='nb'></span><span class='ns'></span><span class='wb'></span><span class='ws'></span><span class='nb'></span><span class='ns'></span><span class='nb'></span><span class='ns'></span><span class='nb'></span><span class='ns'></span><span class='wb'></span><span class='ns'></span><span class='nb'></span><span class='ns'></span><span class='wb'></span><span class='ws'></span><span class='nb'></span><span class='ns'></span><span class='nb'></span><span class='ns'></span><span class='nb'></span><span class='ns'></span><span class='wb'></span><span class='ns'></span><span class='wb'></span><span class='ws'></span><span class='nb'></span><span class='ns'></span><span class='wb'></span><span class='ns'></span><span class='nb'></span><span class='ws'></span><span class='nb'></span><span class='ns'></span><span class='wb'></span><span class='ns'></span><span class='nb'></span><span class='ns'></span><span class='nb'></span><span class='ns'></span><span class='nb'></span><span class='ws'></span><span class='wb'></span><span class='ns'></span><span class='wb'></span><span class='ns'></span><span class='nb'></span><span class='ns'></span><span class='wb'></span><span class='ns'></span><span class='nb'></span><span class='ws'></span><span class='wb'></span><span class='ns'></span><span class='nb'></span><span class='ns'></span><span class='nb'></span><span class='ns'></span><span class='nb'></span><span class='ns'></span><span class='wb'></span><span class='ws'></span><span class='nb'></span><span class='ns'></span><span class='nb'></span><span class='ns'></span><span class='wb'></span><span class='ns'></span><span class='nb'></span><span class='ws'></span><span class='nb'></span><span class='ns'></span><span class='wb'></span><span class='ns'></span><span class='wb'></span><span class='ns'></span><span class='nb'></span><span class='ns'></span><br>TEST8052</div>
Example:
Barcode http://img689.imageshack.us/img689/4360/barcodeg.jpg
This code was created using HTML Bar Codes, also a php class. You could either
- call this via command line interface and embed it into your normal html for every html-page (for dynamic barcodes)
- create your code once using cli/server and put it into your html hard coded (only suitable if your barcode shall not change with every html-page)
I cannot image any other possiblities.
I find barcodes made up with fonts amounts to a terrible hack. I suggest generating PostScript proper: http://www.terryburton.co.uk/barcodewriter/
Hmm, well, the html2ps user guide has a section named "The font block" that states:
Currently, html2ps recognizes the fonts: Times, New-Century-Schoolbook, Helvetica, Helvetica-Narrow, Palatino, Avantgarde, Bookman, and Courier.
It then goes on to explain how one defines a new font type for use in html2ps. Perhaps this could help?
Maybe you don't need to add barcode support to html2ps, you just have to generate html pages with images of the barcodes you need.
You could use a project like this one to achieve this :barcode-generator or GNU Barcode
Using GNU barcode, this is what I'd do :
- Generate the barcode I need in EPS format.
- Use imagemagick to convert it to an html friendly format, like PNG
- Use the generated image in the html page.
All this seem easily doable/scriptable to me, I can provide more help in that direction if you need.
精彩评论