使用reportlab和PyPDF2合并PDF会丢失图像和嵌入字体

时间:2018-02-02 17:49:10

标签: python image pdf reportlab pypdf2

我正在尝试将现有PDF存储在AWS上,将其读入我的后端(Django 1.1,Python 2.7)并将文本添加到边距中。我当前的代码成功地接受了PDF并将文本添加到边距,但它破坏了PDF:

在浏览器中打开时:

  1. 删除图片
  2. 偶尔会在单词之间添加字符
  3. 偶尔会完全更改PDF的字符集
  4. 在Adobe中打开时:

    1. 说“无法提取嵌入字体'无论字体名称是什么'。有些 许多字符无法正确显示或打印“
    2. 说“发生了绘图错误”
    3. 如果有预编辑的图片,说“数据不足” 图像“
    4. 我使用/不使用预定义字体和/或不使用图像制作了我自己的PDF。具有预定义字体且没有图像的图像按预期工作,但是图像会抛出“读取流时出错”。在Adobe中打开时,只是不在浏览器中显示图像。我得出结论,丢失字体是字符出现问题的原因,但我不确定为什么图像没有显示。

      我无法控制我正在编辑的PDF内容,所以我无法确保他们只使用预定义的字体,他们肯定需要在其中包含图像。以下是我的代码

      from reportlab.pdfgen import canvas
      
      from PyPDF2 import PdfFileWriter, PdfFileReader
      from StringIO import StringIO
      
      class DownloadMIR(APIView):
          permission_classes = (permissions.IsAuthenticated,)
      
          def post(self, request, format=None):
              data = request.data
      
              file_path = "some_path"
              temp_file_path = "some_other_path"
      
              # read your existing PDF
      
              if default_storage.exists(file_path):
                  existing_pdf = PdfFileReader(default_storage.open(file_path, 'rb'))
              else:
                  raise Http404("could not find pdf")
      
              packet = StringIO()
              # create a new PDF with Reportlab
              can = canvas.Canvas(packet)
              height, width = int(existing_pdf.getPage(0).mediaBox.getUpperRight_x()), int(
                  existing_pdf.getPage(0).mediaBox.getUpperRight_y())
              print("width:" + str(width) + " height: " + str(height))
              can.setPageSize([width, height])
              can.rotate(90)
              footer = "Prepared for " + request.user.first_name + " " + request.user.last_name + " on " + datetime.now().strftime('%Y-%m-%d at %H:%M:%S')
              can.setFont("Courier", 8)
              can.drawCentredString(width / 2, -15, footer)
              can.save()
      
              packet.seek(0)
              new_pdf = PdfFileReader(packet)
      
              output = PdfFileWriter()
              for index in range(existing_pdf.numPages):
                  page = existing_pdf.getPage(index)
                  page.mergePage(new_pdf.getPage(0))
                  output.addPage(page)
                  #print("done page " + str(index))
      
              response = HttpResponse(content_type="application/pdf")
      
              response['Content-Disposition'] = 'attachment; filename=' + temp_file_path
      
              output.write(response)
              return response
      

      使用script I found online,我看到有未嵌入的字体。

      Font List
      ['/MPDFAA+DejaVuSansCondensed', '/MPDFAA+DejaVuSansCondensed-Bold
      ', '/MPDFAA+DejaVuSansCondensed-BoldOblique', '/MPDFAA+DejaVuSans
      Condensed-Oblique', '/ZapfDingbats']
      
      Unembedded Fonts
      set(['/MPDFAA+DejaVuSansCondensed-Bold', '/ZapfDingbats', '/MPDFA
      A+DejaVuSansCondensed-BoldOblique', '/MPDFAA+DejaVuSansCondensed'
      , '/MPDFAA+DejaVuSansCondensed-Oblique'])
      

      问题是这些 - 有没有办法从原始PDF中提取嵌入字体并将其嵌入到新的pdf中;是否有一些我没有做的正确导致图像不嵌入?

1 个答案:

答案 0 :(得分:0)

经过一些测试后,我发现问题不在于生成的PDF,而是将PDF作为响应返回。如果我将PDF保存到存储桶并从AWS CLI下载,则可以正常工作。我没有弄清楚如何修复响应以正确地将PDF发送回前端。

相关问题