Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doc: provide an usage example of combining fpdf2 with python-barcode #720

Closed
Lucas-C opened this issue Mar 13, 2023 · 20 comments · Fixed by #746
Closed

Doc: provide an usage example of combining fpdf2 with python-barcode #720

Lucas-C opened this issue Mar 13, 2023 · 20 comments · Fixed by #746

Comments

@Lucas-C
Copy link
Member

Lucas-C commented Mar 13, 2023

We already have a page about Barcodes in our documentation.
It was recently reported that we don't support code 128 barcodes: #719

python-barcode seems like a great library that can produces images for many barcode formats, using Pillow.

It would be interesting to provide a documentation section on how to generate barcodes with python-barcode and embed them in PDFs using fpdf2. Priority should be given to documenting how to insert them as SVG, as it will produce smaller PDF documents thant embedding barcodes as rater images.


By implementing this feature you, as a benevolent FLOSS developper, will provide access to the large community of fpdf2 users to a useful PDF functionality.
As a contributor you will get review feedbacks from maintainers & other contributors, and learn about the lifecycle & structure of a Python library on the way.
You will also be added into the contributors list & map.

This issue can count as part of hacktoberfest

@shindodkar
Copy link

I would like to work on this project.✔

@Lucas-C
Copy link
Member Author

Lucas-C commented Mar 27, 2023

I would like to work on this project.✔

Hi and welcome @shindodkar!

The first step to work on thi would be to read the Development guidelines.

Regarding this task, the goal will be to submit a Pull Request:

This PR will be an update of the Markdown file https://github.com/PyFPDF/fpdf2/blob/master/docs/Barcodes.md.
The idea is to add a new section that explains how to combine both libraries, fpdf2 & python-barcode.

Of course, prior to this Pull Request, you will have to experiment yourself on how to do such thing 😄
This documentation page would be a good starting point: https://python-barcode.readthedocs.io/en/stable/getting-started.html#usage
SVGWriter should be the prefered approach, as it will generate smaller PDF documents.

Also: No such thing as a stupid question: feel free to ask anything there if things are unclear!

@Lucas-C
Copy link
Member Author

Lucas-C commented Mar 27, 2023

Hi @shindodkar

I see that you are requesting help from other contributors:

Could you please wait for those users to answer before posting any more comments on this repo?
People can be slow to respond on GitHub, and that is totally fine.
Please stop solicitating help from users in comments until you get an answer from those original requests 😅

Also, I would advise to:

  1. carefuly read my comment just above, in order to get a grasp of how things works by yourself
  2. if you are still a bit lost, please take the time to redact precise questions, and post them in a new discussion: https://github.com/PyFPDF/fpdf2/discussions

@ssavi-ict
Copy link

ssavi-ict commented Mar 28, 2023

Hi @Lucas-C . How are you doing?
I have gone through this Issue. I read the Barcode page and tried to understand what are actually trying to say about this issue. What I understand from that page is -

  1. Generating a Code 39 is already included in the fpdf2. Hence the first example Code 39 shows directly converting a text to Barcode in Code 39 format.
  2. The same has been done for the Interleaved 2 of 5 method too.
  3. For the example of PDF-417 the example has been used pdf417 package including the implication of fpdf2.
  4. The same has been done for the QRCode and DataMatrix examples.

Please correct me if I am wrong till now.

What I have understood by reading this issue is To provide some documentation and sample code similar to the 3rd and 4th as I have mentioned above.

Am I correct?
If it is I would like to give it a try. However, I am not sure about I have clearly understood this line Priority should be given to documenting how to insert them as SVG, as it will produce smaller PDF documents than embedding barcodes as rater images.

Thank You.

@Lucas-C
Copy link
Member Author

Lucas-C commented Mar 29, 2023

Am I correct?

Yes, you are right 😊
The goal is indeed to provide some documentation and sample code similar on how to use the python-barcode lib with fpdf2

However, I am not sure about I have clearly understood this line: Priority should be given to documenting how to insert them as SVG, as it will produce smaller PDF documents than embedding barcodes as rater images.

As can be seen on the page https://python-barcode.readthedocs.io/en/stable/getting-started.html#usage
there are 2 main approaches to generate images with python-barcode:

  1. using from barcode.writer import SVGWriter
  2. using from barcode.writer import ImageWriter

What I meant is simply that the fpdf2 documentation added to close this issue should focus on the 1st approach, using SVGWriter 😊

@ssavi-ict
Copy link

ssavi-ict commented Mar 29, 2023

Hello @Lucas-C , Thank you for the reply.

I was trying to create a snippet that may fix this issue. However, before that, I tried the following snippet -

from fpdf import FPDF

pdf = FPDF()
pdf.add_page()
pdf.image("vector.svg")
pdf.output("doc-with-svg.pdf")

from this example that converts an SVG image (vector.svg) into a PDF (doc-with-svg.pdf). It is showing the following errors -

C:\Python310\python.exe C:\Users\user\Documents\GitHubRepo\BarCode\main.py 
Traceback (most recent call last):
  File "C:\Users\user\Documents\GitHubRepo\BarCode\main.py", line 20, in <module>
    pdf.image("vector.svg")
  File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\fpdf\fpdf.py", line 262, in wrapper
    return fn(self, *args, **kwargs)
  File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\fpdf\fpdf.py", line 3746, in image
    return self._vector_image(img, x, y, w, h, link, title, alt_text)
  File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\fpdf\fpdf.py", line 3875, in _vector_image
    svg = SVGObject(img.getvalue())
  File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\fpdf\svg.py", line 646, in __init__
    self.convert_graphics(svg_tree)
  File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\fpdf\svg.py", line 696, in convert_graphics
    self.build_group(root_tag, base_group)
  File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\fpdf\svg.py", line 888, in build_group
    pdf_group.add_item(self.build_group(child))
  File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\fpdf\svg.py", line 892, in build_group
    pdf_group.add_item(getattr(ShapeBuilder, shape_tags[child.tag])(child))
  File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\fpdf\svg.py", line 339, in rect
    width = resolve_length(tag.attrib.get("width", 0))
  File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\fpdf\svg.py", line 115, in resolve_length
    raise ValueError(
ValueError: 100% uses unsupported relative length %

Is there anything I need to apply before running this snippet? Is this a Bug?
Should I report a Bug? To create a PDF with from barcode.writer import SVGWriter I faced the same issue.

However, I tried the following snippet -

from fpdf import FPDF
from barcode import Code128
from barcode.writer import ImageWriter

# Create a new PDF document
pdf = FPDF()
pdf.add_page()

code128_img = Code128("1234567890", writer=ImageWriter())
code128_img.save('vector')
pdf.image('vector.png')
pdf.output('new.pdf')

It worked fine. And I have successfully created a PDF named new.pdf with FPDF. Will it be OK?
Waiting for your kind response.

Thanks.

@Lucas-C
Copy link
Member Author

Lucas-C commented Mar 29, 2023

Is there anything I need to apply before running this snippet? Is this a Bug?
Should I report a Bug?

You did everything fine 😊
Our SVG parser does not currently support the full SVG specification:

What matters in the content of the vector.svg file.
What SVG file did you try to embed?
If you can provide it, I'd be happy to have a look at it 😊

This error comes from here: https://github.com/PyFPDF/fpdf2/blob/2.7.1/fpdf/svg.py#L114
We could probably support %-lengths defined in CSS...
I would welcome a PR adding support for this!

@ssavi-ict
Copy link

ssavi-ict commented Mar 30, 2023

@Lucas-C
This is the file I was trying to embed vector.svg

I have created this file using the below snippet -

code128_img = Code128("1234567890", writer=SVGWriter())
code128_img.save('vector')

@Lucas-C
Copy link
Member Author

Lucas-C commented Mar 30, 2023

Thank you, I'll have a look at it this evening! 😊

@ssavi-ict
Copy link

Hi @Lucas-C , OK. Please let me know.

Can

However, I tried the following snippet -

from fpdf import FPDF
from barcode import Code128
from barcode.writer import ImageWriter

# Create a new PDF document
pdf = FPDF()
pdf.add_page()

code128_img = Code128("1234567890", writer=ImageWriter())
code128_img.save('vector')
pdf.image('vector.png')
pdf.output('new.pdf')

However, will it be OK?

@Lucas-C
Copy link
Member Author

Lucas-C commented Mar 30, 2023

I fixed the ValueError you reported in #744

You can check that your last code snippet now works (the one with the call to SVGWriter), using the latest version of fpdf2 from this repository master branch:

pip install git+https://github.com/PyFPDF/fpdf2.git@master

@ssavi-ict
Copy link

ssavi-ict commented Mar 30, 2023

Thanks for the Quick fix @Lucas-C , Works fine. Except for the following warning -
UserWarning: <svg> has no "viewBox", using its "width" & "height" as default "viewBox" warnings.warn(
Which I think can be Ignored.

However, I have created the 128 Barcode PDF using fpdf and python-barcode and am ready to push the changes to the Barcode.md. Could you please share in which files should I reflect the changes along with the Barcode.md file?

Thank You.

@Lucas-C
Copy link
Member Author

Lucas-C commented Mar 30, 2023

Could you please share in which files should I reflect the changes along with the Barcode.md file?

Please just add a mention of this additon in the doc to CHANGELOG.md!

@ssavi-ict
Copy link

@Lucas-C Thank You. I will do it by tonight.

@ssavi-ict
Copy link

ssavi-ict commented Mar 31, 2023

@Lucas-C , with these small contributions, I would like to contribute features/resolve bugs in the Codebase as well. What are the relevant knowledge and learnings I should gather in case of contributing to including new features and/or resolving bugs?

Thank You.

@Lucas-C
Copy link
Member Author

Lucas-C commented Mar 31, 2023

@Lucas-C , with these small contributions, I would like to contribute features/resolve bugs in the Codebase as well. What are the relevant knowledge and learnings I should gather in case of contributing to including new features and/or resolving bugs?

Most information is on https://pyfpdf.github.io/fpdf2/Development.html

I also gave some general advice there: #496 (comment)

@ssavi-ict
Copy link

ssavi-ict commented Mar 31, 2023

Thank You @Lucas-C .
I think the docs\ directory should be a bit tidy.
All the images, demo py files, Tutorials, etc should have some designated locations like image\, tutorials\, demo_codes\ etc.

@Lucas-C
Copy link
Member Author

Lucas-C commented Mar 31, 2023

Thank You @Lucas-C . I think the docs\ directory should be a bit tidy. All the images, demo py files, Tutorials, etc should have some designated locations like image\, tutorials\, demo_codes\ etc.

Why not, this seems like a good idea to me 😊
Would you like to submit a PR that improves things as you suggest?

@ssavi-ict
Copy link

ssavi-ict commented Apr 1, 2023

@Lucas-C , I will be more than happy to do this. It may take some time.
And I will be needing your help doing this. As the documentation stuff depends on those files.
Also, I think it will be good if we open a new thread to track these improvements.

Can you give me an idea of which sort of things I need to take care of to stack up things in the docs/ folder?

@Lucas-C
Copy link
Member Author

Lucas-C commented Apr 3, 2023

Thanks to @ssavi-ict, a documentation section has now been added:
https://pyfpdf.github.io/fpdf2/Barcodes.html#code128

A bugfix was spotted in the process of redacting this documentation section,
and a fix has just been released in v2.7.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants