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: 102

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.
 

Pablo Torres

Active Member
Licensed User
Longtime User

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

View attachment 162922
Sorry Erel but when I run this code in my machine it throws an error, I have tried this for several days and I dont know what I'm doing wrong.
The code is exactly the one you posted, I did not made any changes and here is the logs:

<logs>
Waiting for debugger to connect...
Program started.
Server is listening on port: 52234
Python path: C:\Program Files\Anywhere Software\B4J\libraries\Python\python\python.exe
connected
starting PyBridge v1.00
watchdog set to 30 seconds
Connecting to port: 52234
Error occurred on line: 130 (PyWrapper)
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
at b4j.example.pywrapper._getvalue(pywrapper.java:106)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at jdk.internal.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
at anywheresoftware.b4a.shell.DebugResumableSub$RemoteResumableSub.resume(DebugResumableSub.java:21)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:156)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:105)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
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.shell.ShellBA.startMessageLoop(ShellBA.java:121)
at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:181)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:309)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
at b4j.example.main.main(main.java:29)
Caused by: java.lang.RuntimeException: (main._htmltopdf) - Python Error (ModuleNotFoundError) - Method: module.exec: No module named 'fpdf'
at b4j.example.pywrapper.raiseError(pywrapper.java:1042)
... 39 more
</logs>
 

Pablo Torres

Active Member
Licensed User
Longtime User
Same error stands

(main._htmltopdf) - Python Error (ModuleNotFoundError) - Method: module.exec: No module named 'fpdf'
 

Theera

Expert
Licensed User
Longtime User
I'm sorry my mistake.

'After Click the global Python linked ,Type pip install fpdf2
'กรณี เผลอติดตั้ง fpdf แทน fpdf2 ต้องทำการยกเลิก แล้วติดตั้งไลบารี่ที่ถูกลงไปแทน ด้วย 2 ชุดคำสั่ง
'pip uninstall fpdf
'อย่าลืม กดปุ่ม y เพื่อยืนยันการยกเลิกติดตั้งก่อนติดตั้งอันใหม่
'pip install fpdf2
After you click the global Python linked ,you must type pip install fpdf2 ,so you must type pip uninstall fpdf before and then click "y" button ,the last you type pip install fpdf2 again. The result is within object folder.
 
Top