B4J Code Snippet [PyBridge] Create pdf with fpdf2


This is a console app, based on the console template: [PyBridge] Console (non-ui) project template

Dependency: pip install fpdf2
B4X:
'Html - html string
'img - Background image. Can be array of bytes or a string with the path to the image file.
Public Sub HtmlToPDF (Html As Object, img As Object) As PyWrapper
    Dim Code As String = $"
import fpdf

def HtmlToPDF (Html, img):
    pdf = fpdf.FPDF()
    pdf.add_page()
    if not img is None:
        pdf.image(img, x=0, y=0, w=210, h=297)
    pdf.write_html(Html)
    return pdf.output()
"$
    Return Py.RunCode("HtmlToPDF", Array(Html, img), Code)
End Sub

1742967903293.png
 

Attachments

  • fpdf2.zip
    14.5 KB · Views: 23

Theera

Expert
Licensed User
Longtime User
Hi Erel,
After I've downloaded example for learning,I clicked the global python linked for install fpdf (pip install fpdf2) and ran it. I've error message.
How do I solve it,please?

Server is listening on port: 51672
Python path: C:\Python\Python313\python.exe
connected
starting PyBridge v1.00
watchdog set to 30 seconds
Connecting to port: 51672
main._appstart (java line: 44)
java.lang.RuntimeException: java.lang.RuntimeException: (main._htmltopdf) - Python Error (TypeError) - Method: function.__call__: argument should be integer or bytes-like object, not 'str'
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:140)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
at anywheresoftware.b4a.keywords.Common$3.run(Common.java:1172)
at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:47)
at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:43)
at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:181)
at b4j.example.main._appstart(main.java:44)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:578)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
at b4j.example.main.main(main.java:28)
Caused by: java.lang.RuntimeException: (main._htmltopdf) - Python Error (TypeError) - Method: function.__call__: argument should be integer or bytes-like object, not 'str'
at b4j.example.pywrapper.raiseError(pywrapper.java:623)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:578)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
at b4j.example.pywrapper._getvalue(pywrapper.java:248)
at b4j.example.main$ResumableSub_Start.resume(main.java:204)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:156)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:105)
... 11 more
 

Theera

Expert
Licensed User
Longtime User
I'm sorry that I found my problem. I installed fpdf instead of fpdf2.
 
Top