在pdf页脚添加页码

问题描述 投票:0回答:1

我需要将页码添加到pdf的页脚。已经尝试过 pypdf 和 reportlab。我不知道如何实现它。

  1. 为reportlab生成数据以从数据库创建表
  2. 使用reportlab创建表格
  3. 使用 PyPDF2 迭代页面并添加注释
  4. 将文件从服务器返回给客户端

客户端工作的文件生成和检索。缺少的是添加 y 页 x 的部分

def create_sample_source_report_pdf(task_id):
...... data generation ..........

  # Create a byte stream to hold the PDF content
  pdf_byte_stream = io.BytesIO()
  
  # Create a PDF document
  doc = SimpleDocTemplate(pdf_byte_stream, pagesize=landscape(A4))

  # Create a table with the data
  table = Table(data)
  
  # Add style to the table
  style = TableStyle([
      ('TEXTCOLOR', (0, 0), (-1, 0), colors.white),  # Header text color
      ('BACKGROUND', (0, 0), (-1, 0), colors.darkgrey),  # Header background color
      ('ALIGN', (0, 0), (-1, -1), 'CENTER'),
      ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
      ('BOTTOMPADDING', (0, 0), (-1, 0), 12),
      ('BACKGROUND', (0, 1), (-1, -1), colors.lightgrey),  # Light grey background for every 2nd row
      ('GRID', (0, 0), (-1, -1), 1, colors.black),
  ])
  
  table.setStyle(style)

  # Build the PDF
  doc.build([table])  # Spacer for title

  # Move the pointer to the beginning of the byte stream
  pdf_byte_stream.seek(0)

  ##################add text with page x of y


  # Now you can use the modified byte stream to create a BlobMedia
  blob_media_pdf = BlobMedia("application/pdf", modified_pdf_byte_stream.read(), name="sample_source_list_with_page_numbers.pdf")

  return blob_media_pdf
python pypdf reportlab
1个回答
0
投票

找到了一种用文本创建新页面,然后将其与 pypdf 合并的方法。

from pypdf import PdfReader, PdfWriter, PdfMerger, PageObject
from reportlab.lib.pagesizes import landscape, A4
from reportlab.pdfgen.canvas import Canvas
from reportlab.pdfgen import canvas
from reportlab.platypus import Image
from reportlab.lib.utils import ImageReader


 # Create a byte stream to hold the PDF content
  pdf_byte_stream = io.BytesIO()
  
  ##########instead of pdf creation every kind of pdf can be loaded#########

  # Create a table with the data and style, and repeat the header row
  table = Table(data, style=style, repeatRows=1)
  
  # Build the PDF
  doc = SimpleDocTemplate(pdf_byte_stream, pagesize=landscape(A4))
  doc.build([table])
  
  # Move the pointer to the beginning of the byte stream
  pdf_byte_stream.seek(0)
  
  # Read the generated PDF with PyPDF2
  pdf_reader = PdfReader(pdf_byte_stream)
  
  # Create a PdfWriter to hold the modified PDF
  pdf_writer = PdfWriter()
  
  for page_number, page in enumerate(pdf_reader.pages):
      # Create a new packet for each page
      packet = io.BytesIO()
      can = canvas.Canvas(packet, pagesize=landscape(A4))
      #firs is the x ax, 2nd i y
      can.drawString(750, 30, f"Page: {page_number + 1} of {len(pdf_reader.pages)}")
      can.save()
  
      # Move to the beginning of the StringIO buffer
      packet.seek(0)
  
      # Read the new PDF with pypdf
      new_pdf = PdfReader(packet)
      
      # Merge the new page with the original page
      page.merge_page(new_pdf.pages[0])
      
      # Add the merged page to the PdfWriter
      pdf_writer.add_page(page)

    
  # Create a byte stream to hold the modified PDF content
  modified_pdf_byte_stream = io.BytesIO()
  
  # Write the modified PDF to the new byte stream
  pdf_writer.write(modified_pdf_byte_stream)
  
  # Move the pointer to the beginning of the modified byte stream
  modified_pdf_byte_stream.seek(0)
  
  # Now you can use the byte stream to create a BlobMedia
  blob_media_pdf = BlobMedia("application/pdf", modified_pdf_byte_stream.read(), name="sample_source_list_with_page_numbers.pdf")
  
  # Return the BlobMedia object
  return blob_media_pdf
© www.soinside.com 2019 - 2024. All rights reserved.