Hi All,
I wrote some pieces of code to implement pdf:barcode to work.
Example:
<pdf:barcode value="EMBED CODE HERE" type="code128" humanreadable="1"
barwidth="0.33mm" barheight="2cm" align="top">
Some incovenience still exists, so I hope somebody refine the code...
Each patch sould be applied in $PISA_PATH/sx/pisa3/ .
--- pisa_default.py.orig 2010-04-12 20:56:42.282101000 +0900
+++ pisa_default.py 2010-04-19 21:49:08.891460800 +0900
@@ -176,7 +176,22 @@
"pdfbarcode": (0, {
"value": (STRING, MUST),
- "align": (["left", "center", "right"],
"left"),
+ "type": (["i2of5", "itf",
+ "code39", "extendedcode39",
+ "code93", "extendedcode93",
+ "msi",
+ "codabar", "nw7",
+ "code11",
+ "fim",
+ "postnet",
+ "usps4s",
+ "code128",
+ "ean13", "ean8",
+ ], "code128"),
+ "humanreadable": (STRING, ""),
+ "barwidth": SIZE,
+ "barheight": SIZE,
+ "align": (["baseline", "top", "middle",
"bottom"], "baseline"),
}),
# ========================================================
--- pisa_tags.py.orig 2010-02-02 13:53:46.285384000 +0900
+++ pisa_tags.py 2010-04-19 22:00:26.925320800 +0900
@@ -12,7 +12,8 @@
from pisa_reportlab import *
from pisa_util import *
-from reportlab.graphics.barcode.code39 import Standard39
+from reportlab.graphics.barcode import createBarcodeDrawing
+
from reportlab.lib.styles import ParagraphStyle
from reportlab.platypus.flowables import *
from reportlab.platypus.paraparser import tt2ps, ABag
@@ -575,17 +576,88 @@
c.loadFont(
self.attr.name, self.attr.src, self.attr.encoding)
class pisaTagPDFBARCODE(pisaTag):
- """
- <pdf:barcode value="" align="">
- """
- def start(self, c):
- c.addPara()
- attr = self.attr
- bc = Standard39()
- bc.value = attr.value
- bc.barHeight = 0.5 * inch
- bc.lquiet = 0 # left padding
- bc.rquiet = 0 # left padding
- bc.hAlign = attr.align.upper()
- c.addStory(bc)
- c.addPara()
+
+ _codeName = {
+ "I2OF5": "I2of5",
+ "ITF": "I2of5",
+ "CODE39": "Standard39",
+ "EXTENDEDCODE39": "Extended39",
+ "CODE93": "Standard93",
+ "EXTENDEDCODE93": "Extended93",
+ "MSI": "MSI",
+ "CODABAR": "Codabar",
+ "NW7": "Codabar",
+ "CODE11": "Code11",
+ "FIM": "FIM",
+ "POSTNET": "POSTNET",
+ "USPS4S": "USPS_4State",
+ "CODE128": "Code128",
+ "EAN13": "EAN13",
+ "EAN8": "EAN8",
+ }
+
+ class _barcodeWrapper(Flowable):
+ """Wrapper for barcode widget
+ """
+ def __init__(self, codeName="Code128", value="", **kw):
+ self.widget = createBarcodeDrawing(codeName, value=value,
**kw)
+
+ def draw(self, canvas, xoffset=0, **kw):
+ # NOTE: `canvas' is mutable, so canvas.restoreState() is
a MUST.
+ canvas.saveState()
+ canvas.translate(xoffset, 0)
+ self.widget.canv = canvas
+ self.widget.draw()
+ canvas.restoreState()
+
+ def wrap(self, aW, aH):
+ return self.widget.wrap(aW, aH)
+
+ def start(self, c):
+ attr = self.attr
+ codeName = attr.type or "Code128"
+ codeName =
pisaTagPDFBARCODE._codeName[codeName.upper().replace("-", "")]
+ humanReadable = bool(attr.humanreadable)
+ barWidth = attr.barwidth or 0.01*inch
+ barHeight = attr.barheight or 0.5*inch
+
+ # Assure minimal size.
+ barWidth = max(barWidth, 0.33*mm if codeName in ("EAN13",
"EAN8") else 0.0075*inch)
+ #barHeight = max(barHeight, 25.93*mm)
+
+ barcode = pisaTagPDFBARCODE._barcodeWrapper(
+ codeName=codeName,
+ value=attr.value,
+ barWidth=barWidth,
+ barHeight=barHeight,
+ humanReadable=humanReadable,
+ fontName="Courier",
+ )
+
+ width, height = barcode.wrap(c.frag.width, c.frag.height)
+
+ #barcode.spaceBefore = c.frag.spaceBefore
+ #barcode.spaceAfter = c.frag.spaceAfter
+
+ c.force = True
+
+ valign = attr.align or c.frag.vAlign or "baseline"
+ if valign in ["texttop"]:
+ valign = "top"
+ elif valign in ["absmiddle"]:
+ valign = "middle"
+ elif valign in ["absbottom", "baseline"]:
+ valign = "bottom"
+
+ afrag = c.frag.clone()
+ afrag.text = ""
+ afrag.fontName="Courier"
+ afrag.cbDefn = ABag(
+ kind="barcode",
+ barcode=barcode,
+ width=width,
+ height=height,
+ valign=valign,
+ )
+ c.fragList.append(afrag)
+
--- reportlab_paragraph.py.orig 2010-04-19 21:18:06.454306000 +0900
+++ reportlab_paragraph.py 2010-04-19 21:47:18.062608200 +0900
@@ -218,6 +218,19 @@
cur_x += w
cur_x_s += w
setXPos(tx,cur_x_s-tx._x0)
+ elif kind=='barcode':
+ barcode = cbDefn.barcode
+ w = cbDefn.width
+ h = cbDefn.height
+ txfs = tx._fontsize
+ if txfs is None:
+ txfs = xs.style.fontSize
+ iy0, iy1 = imgVRange(h, cbDefn.valign, txfs)
+ cur_x_s = cur_x + nSpaces*ws
+ barcode.draw(canvas=tx._canvas, xoffset=cur_x_s)
+ cur_x += w
+ cur_x_s += w
+ setXPos(tx, cur_x_s-tx._x0)
else:
name = cbDefn.name
if kind=='anchor':
--
Sie haben diese Nachricht erhalten, da Sie der Google Groups-Gruppe Pisa XHTML2PDF Support beigetreten sind.
Wenn Sie Nachrichten in dieser Gruppe posten möchten, senden Sie eine E-Mail an
xhtm...@googlegroups.com.
Wenn Sie aus dieser Gruppe austreten möchten, senden Sie eine E-Mail an
xhtml2pdf+...@googlegroups.com.
Besuchen Sie die Gruppe unter
http://groups.google.com/group/xhtml2pdf?hl=de, um weitere Optionen zu erhalten.