need to hack code39 behaviour #971
Replies: 4 comments 4 replies
-
I don't have access to ISO/IEC 16388, but all the descriptions of code39 I can find say that the ratio should be between 2:1 and 3:1 (recommended), so I suspect that is exactly what the standard says as well. Of course you're free to subclass FPDF and overwrite the |
Beta Was this translation helpful? Give feedback.
-
Yes, that's what I want to do (implementing also the standard ratio limits you told me about). I'd like to apply the changes within my script, without touching the installed
I guess something like this is the most proper way to do it? fpdf_modified.py: from fpdf import FPDF
#from fpdf import check_page # @check_page is a decorator of code39() in FPDF. How do I use it here?
#@check_page # commented because the above import doesn't work
def _new_code39(self, text, x, y, w=1.5, h=5, ratio=3):
"""Barcode 3of9 (modified with non-forced, optional user-defined ratio)"""
if ratio>3 or ratio<2: # https://en.wikipedia.org/wiki/Code_39
ratio=3 # use our default to keep within the standard
dim = {"w": w, "n": w / ratio}
(...)
FPDF.code39 = _new_code39 # this rewrites the original function so myscript.py can keep using its name myscript.py: from fpdf_modified import FPDF # imported from the above script
pdf = FPDF(orientation="P", unit="mm", format="A4")
pdf.add_page()
pdf.code39("*fpdf modified code39*", x=30, y=50, w=1.5, h=20, ratio=2.8)
pdf.output("fpdf-hacked-code39-test.pdf") Although the above code seems to work, I still have some questions:
Thanks a lot again for your time |
Beta Was this translation helpful? Give feedback.
-
Oops, I just realized that I've been leading you on the wrong path with So the simples way is to just leave it away. After all, its only purpose is to give you an easier to understand error message if you try to add some content to a document with no pages added yet. |
Beta Was this translation helpful? Give feedback.
-
No problem. With this discussion I got examples on how to do things in Python. Thanks for all your detailed explanations! |
Beta Was this translation helpful? Give feedback.
-
I see this thread is the only discussion about code39 barcodes.
In the fpdf-based php script I am trying to port to fpdf2-python, there was a very tiny modification of Code39().
So that I could choose the line proportions in generated barcodes (I can't recall exactly how I decided to do this but I think that made barcodes easier to read with my mobile, at least for a particular label size I needed to print some time ago).
This was a hack made by me and I think the equivalent change in python would only affect the beginning of code39() definition:
https://github.com/py-pdf/fpdf2/blob/aa3b1f4a1e16b1d513e5064278b2fb2f5b2747af/fpdf/fpdf.py#L4517C1-L4519C35
Current definition:
Modified version:
I have very low Python programming skills but I think this change wouldn't break compatibility with previous versions.
I doubt this hack can be useful for someone else (I don't see myself improving barcodes technology), but I leave that to you as a possible think to change.
Anyway, I need to do this hack in order to port my php script, so I ask your opinion about the most correct/easiest way to do this in Python.
How can I do this right in my script, without touching the installed
fpdf2
package?So when I upgrade
fpdf2
version, my script can keep working and using my modifiedcode39()
version.(at risk of missing only other possible improvements of that specific
code39()
function whichfpdf2
can make in the future)Thanks a lot in advance for your suggestions
@abubelinha
Beta Was this translation helpful? Give feedback.
All reactions