【问题标题】:convert multiple files from base64 to pdf in memory在内存中将多个文件从base64转换为pdf
【发布时间】:2022-09-27 22:57:42
【问题描述】:

我收到几个base64格式的文件,我需要以pdf格式将它们上传到aws s3,但到目前为止我已经尝试了所有方法,但我仍然无法做到,有没有办法在不创建文件的情况下将它们转换为pdf ?

我正在使用 django 休息框架

      \"balance\":\"base64String\",
      \"stateOfCashflow\":\"base64String\",
      \"financialStatementAudit\":\"base64String\",
      \"managementReport\":\"base64String\",
      \"certificateOfStockOwnership\":\"base64String\",
      \"rentDeclaration\":\"base64String\",
  • 最后你在某些时候错了,我找到了一个 django 库,它完全符合我的需要。

标签: django amazon-s3 django-rest-framework


【解决方案1】:

我自己解决了这个问题,我找到了一个名为 drf_extra_fields 的库,它可以满足我的需要。

在序列化程序中需要使用base64filefield,它采用base 64中的字符串并将其转换为幕后的pdf

from drf_extra_fields.fields import Base64FileField
import PyPDF2
import io


class PDFBase64File(Base64FileField):
    ALLOWED_TYPES = ['pdf']

    def get_file_extension(self, filename, decoded_file):
        try:
            PyPDF2.PdfFileReader(io.BytesIO(decoded_file))
        except PyPDF2.utils.PdfReadError as e:
            print(e)
        else:
            return 'pdf'

class PDFSerializer(serializers.ModelSerializer):
    pdf = PDFBase64File()
    class Meta:
        model = pdf
        fields = "__all__"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多