Give external pdf reader direct access to output object in memory? #1284
Replies: 3 comments
-
Hi @prainbow55 First, this is not an issue with The reason for the Now, the underlying problem is that I do not think that Adobe Acrobat Reader can read a PDF document from memory, without reading a file on the filesystem. Where have you found documentation pointing to that? On Windows, if you want to display a PDF file in a window without writing anything to disk, you may want to consider using a web browser rendering a PDF file from a local HTTP endpoint: https://py-pdf.github.io/fpdf2/UsageInWebAPI.html Under Linux, you could use something like |
Beta Was this translation helpful? Give feedback.
-
My question was simply a how-to, There is no issue at all with fpdf2.
Two fairly recent posts on StackOverflow address how to return a pdf file
from a memory buffer.
https://stackoverflow.com/questions/73585779/how-to-return-a-pdf-file-from-in-memory-buffer-using-fastapi
https://stackoverflow.com/questions/76195784/how-to-generate-and-return-a-pdf-file-from-in-memory-buffer-using-fastapi
I know of nothing in Adobe's API on handling such an object. But if the pdf
file can be returned, it seemed reasonable to try to open it in a default
app. My hope grew—since Acrobat is a widely used pdf reader—that the fpdf2
developers might already have come up with a simple solution. If not, no
problem, I have a workaround in mind for now.
Maybe my dream could become an idea for future fpdf2 development?
…On Sun, Oct 13, 2024 at 5:19 AM Lucas Cimon ***@***.***> wrote:
Hi @prainbow55 <https://github.com/prainbow55>
First, this is not an issue with fpdf2, this about usage Adobe Acrobat
Reader.
I will try to give some help, but really this is not my area of
expertise 😅
The reason for the UnicodeDecodeError is that subprocess.run() expects
string arguments, and you are providing bytes.
Now, the underlying problem is that *I do not think that Adobe Acrobat
Reader can read a PDF document from memory, without reading a file on the
filesystem*.
Where have you found documentation pointing to that?
On Windows, if you want to display a PDF file in a window without writing
anything to disk, you may want to consider using a web browser rendering a
PDF file from a local HTTP endpoint:
https://py-pdf.github.io/fpdf2/UsageInWebAPI.html
Under Linux, you could use something like /dev/shm to have a virtual
filesystem that only relies on RAM.
—
Reply to this email directly, view it on GitHub
<#1284 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AX2NJTG3UPESWK6VBOU6K23Z3JCMTAVCNFSM6AAAAABP2VZHH2VHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTAOJSG42DQNA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Hi @prainbow55 Alright, thank you for explaining your intent. As explained previously, this has more to do with Adobe Acrobat Reader capabilities, I do not really see what improvement could be made on As a final note, the Python builtin tempfile module can also be of great use to create a temporary file to be viewed with a PDF reader program: from subprocess import run
from tempfile import NamedTemporaryFile
from fpdf import FPDF
pdf = FPDF()
pdf.add_page()
pdf.set_font("Helvetica", size=12)
pdf.cell(text="Hello world!")
with NamedTemporaryFile(delete_on_close=False) as fp:
# delete_on_close requires Python 3.12
# but a similar behaviour can be implemented with earlier versions of Python
fp.write(pdf.output())
fp.close()
run((acrobat_path, fp.name)) |
Beta Was this translation helpful? Give feedback.
-
How to bypass writing output to disk when sending final pdf to an external pdf reader?
By default, FPDF.output returns a bytearray buffer (https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.output).
To enable a reader like Adobe Acrobat to read this object directly, what manipulations are needed?
Using the basic example from the English Tutorial with slight modifications, I've tried this code without success.
The result is "UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9c in position 333: invalid start byte".
Beta Was this translation helpful? Give feedback.
All reactions