簡體   English   中英

如何在 MySQL 數據庫中存儲 PDF 而不在 ZA7F5F35426B927211FC9231B53Z 中生成 PDF 文件

[英]How to store PDF in MySQL database without generating PDF file in Python

So basically I have a base64 encoded PDF data in MySQL database, And I want to manipulate that data ( Update the form fields of PDF file data), after that without creating/Write a PDF file I want to store that manipulated/updated data into一個數據庫。 Python 代碼如下所示。

在這里我使用PyPDF2並且代碼正在運行

import base64, io, PyPDF2

try:
    data_dict = '{"firstName": "John", "lastName": "Joe"}'
    encodedDataOfPDF = base64.b64decode(data)  #base64 encoded data of pdf from database

    file = io.BytesIO(encodedDataOfPDF)
    pdfReader = PyPDF2.PdfFileReader(file)
    pdfWriter = PyPDF2.PdfFileWriter()
    pdfWriter.appendPagesFromReader(pdfReader)

    #Here form fields of PDF gets updated.
    pdfWriter.updatePageFormFieldValues(pdfWriter.getPage(0), data_dict)  


    #If I uncomment below code then it will create a PDF file with updated data.
    #But I Don't want a PDF File, 
    #I just need the base64 encoded data of that updated/manipulated file which I will store in the Database.

    # with open(data[1], 'wb') as f:
    #     pdfWriter.write(f)


except Exception as e:
    app.logger.info(str(e))

注意:另請閱讀代碼中的注釋

提前致謝。

經過大量研究后,我得到了正確的方法來獲取更新/操縱的編碼數據,稱為 stream。

import base64, io, PyPDF2

try:
    tempMemory = io.BytesIO() #Added BytesIO
    data_dict = '{"firstName": "John", "lastName": "Joe"}'
    encodedDataOfPDF = base64.b64decode(data)  #base64 encoded data of pdf from database

    file = io.BytesIO(encodedDataOfPDF)
    pdfReader = PyPDF2.PdfFileReader(file)
    pdfWriter = PyPDF2.PdfFileWriter()
    pdfWriter.appendPagesFromReader(pdfReader)

    #Here form fields of PDF gets updated.
    pdfWriter.updatePageFormFieldValues(pdfWriter.getPage(0), data_dict)  

    pdfWriter.write(tempMemory)
    newFileData = tempMemory.getvalue()
    newEncodedPDF= base64.b64encode(newFileData) # Here I get what I want.


except Exception as e:
    app.logger.info(str(e))

我得到了 base64 編碼數據,但沒有生成 PDF 文件。

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM