encoding - How to base64 encode a PDF file in Python - Stack Overflow
python - How to convert a PDF from base64 string to a file? - Stack Overflow
python - Decoding binary to pdf - Stack Overflow
Python - How to write a PDF from a BINARY STRING
If you don't want to use the xmlrpclib's Binary class, you can just use the .encode() method of strings:
a = open("pdf_reference.pdf", "rb").read().encode("base64")
Actually, after some more digging, it looks like the xmlrpclib module may have the piece I need with it's Binary helper class:
binary_obj = xmlrpclib.Binary( open('foo.pdf').read() )
Here's an example from the Trac XML-RPC documentation
import xmlrpclib
server = xmlrpclib.ServerProxy("http://athomas:password@localhost:8080/trunk/login/xmlrpc")
server.wiki.putAttachment('WikiStart/t.py', xmlrpclib.Binary(open('t.py').read()))
From my understanding base64decode only takes in a base64 string and looks like you have some headers on your string that are not encoded.
I would remove "data:application/pdf;base64,"
check out the doc here: https://docs.python.org/2/library/base64.html
When I've used it in the past, I have only used the encoded string.
Does writing it by using the codecs.decode function work?
also as Mark stated, you can try to remove the data:application/pdf;base64, portion of the string as this section of the string is not to be decoded.:
import codecs
base64String = "JVBERi0xLjQKJeHp69MKMSAwIG9iago8PC9Qcm9kdWNlciAoU2tpYS9..."
with open("test.pdf", "wb") as f:
f.write(codecs.decode(base64string, "base64"))
Hi...
I already have inserted in oracle database a CLOB column with a base64 text that represents a PDF
I made a SELECT from that column/line and get the text i want.
e.g:
try:cursor.execute("CREATE TABLE lob_tbl (id NUMBER, b CLOB)")conexao.commit()except:pass
'''cursor.execute("insert into lob_tbl (id, b) values (:lobid, :blobdata)",lobid=10, blobdata=Sb64pdf)conexao.commit()'''query = cursor.execute("select b from lob_tbl where id = 10").fetchone()img = query[0].read()
if I take this string that was returned from select and put it in some online converter, it works perfectly and returns exactly the pdf I want
But what i need is to convert that string to a pdf in PYTHON, then i tried something like this:
binario = base64.b64decode(img)with open('result.pdf','wb') as f:f.write(binario)
but when i open the pdf result.pdf it gives me an error, it cant be opened, what is wrong?
As @pvg mentioned in the comments, overriding load_resource function with your base64 functionality does the trick.
import base64,io
def load_resource(self, reason, filename):
if reason == "image":
if filename.startswith("http://") or filename.startswith("https://"):
f = BytesIO(urlopen(filename).read())
elif filename.startswith("data"):
f = filename.split('base64,')[1]
f = base64.b64decode(f)
f = io.BytesIO(f)
else:
f = open(filename, "rb")
return f
else:
self.error("Unknown resource loading reason \"%s\"" % reason)
EDIT :
This is a sample code to insert images into pdf. I commented some instructions in code.
from fpdf import FPDF
import os
import io
import base64
class PDF(FPDF):
def load_resource(self, reason, filename):
if reason == "image":
if filename.startswith("http://") or filename.startswith("https://"):
f = BytesIO(urlopen(filename).read())
elif filename.startswith("data"):
f = filename.split('base64,')[1]
f = base64.b64decode(f)
f = io.BytesIO(f)
else:
f = open(filename, "rb")
return f
else:
self.error("Unknown resource loading reason \"%s\"" % reason)
def sample_pdf(self,img,path):
self.image(img,h=70,w=150,x=30,y=100,type="jpg")
#make sure you use appropriate image format here jpg/png
pdf.output(path, 'F')
if __name__ == '__main__':
img = # pass your base64 image
# you can find sample base64 here : https://pastebin.com/CaZJ7n6s
pdf = PDF()
pdf.add_page()
pdf_path = # give path to where you want to save pdf
pdf.sample_pdf(img,pdf_path)
I've been facing this issue lately and the answer from Uchiha Madara didn't work in my case, so I fixed it in a slightly different way. When I tried it with Uchiha's Method, I the same FileNotFound Error you do if you supply an image without any modification to your code ( without the load_resource function ). Since I really needed a solution and there was no way around, I looked into the module code which can be found in
C:/Users/user/AppData/Local/Programs/Python/Python38/Lib/site-packages/fpdf/fpdf.py
If you look around there for a bit, you notice that the image is imported via the _parsepng function. So, we need to edit this to accept a base64 data string.
Basically, what you need to do to fix it:
In the function, you need to add an elif at the top to check whether the "filename" contains a string indicating it's base64, and you need to import 2 new modules.
Copy & paste this code below the first if-Statement to check for a URL:
elif "data:image/png;base64" in name:
f = name.split('base64,')[1]
f = base64.b64decode(f)
f = io.BytesIO(f)
This just looks for the string which is typical for every base64-encoded image, and if it's there and decodes it.
You need to import the base64 and the io modules at the top of the script, so just do that via
import base64, io
Now just supply your base64 string as the file path like you would do normally, and it should work ( did on my tests with python 3.8 ).
Contact me if you have any questions, I hope I can help some people reading this in the future.