Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PyPDF2 import PdfFileWriter, PdfFileReader
- import io
- from reportlab.pdfgen import canvas
- from reportlab.lib.pagesizes import A4
- from reportlab.lib.units import mm
- from reportlab.lib.styles import ParagraphStyle
- from reportlab.platypus import Paragraph
- my_Style=ParagraphStyle('My Para style',
- fontName='Times-Roman',
- #backColor='#F1F1F1',
- fontSize=8,
- #borderColor='#FFFF00',
- #borderWidth=1,
- #borderPadding=(5,5,5),
- #leading=5,
- alignment=1
- )
- packet = io.BytesIO()
- def coord(x, y, height, unit=1):
- x, y = x * unit, height - y * unit
- return x, y
- c = canvas.Canvas(packet, pagesize=A4)
- width, height = A4
- p1=Paragraph('''<b>blablalblaflasldflasd śćććśćśćśćśćśćśćś</b>''',my_Style)
- p1.wrapOn(c, (*coord(190, 25, height, mm)))
- p1.drawOn(c, (*coord(10, 172, height, mm)))
- c.showPage()
- c.save()
- #move to the beginning of the StringIO buffer
- packet.seek(0)
- # create a new PDF with Reportlab
- new_pdf = PdfFileReader(packet)
- # read your existing PDF
- existing_pdf = PdfFileReader(open("original.pdf", "rb"))
- output = PdfFileWriter()
- # add the "watermark" (which is the new pdf) on the existing page
- page = existing_pdf.getPage(0)
- page.mergePage(new_pdf.getPage(0))
- output.addPage(page)
- # finally, write "output" to a real file
- outputStream = open("destination30.pdf", "wb")
- output.write(outputStream)
- outputStream.close()
Advertisement
Add Comment
Please, Sign In to add comment