Guest User

Untitled

a guest
Nov 9th, 2022
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. from PyPDF2 import PdfFileWriter, PdfFileReader
  2. import io
  3. from reportlab.pdfgen import canvas
  4. from reportlab.lib.pagesizes import A4
  5. from reportlab.lib.units import mm
  6. from reportlab.lib.styles import ParagraphStyle
  7. from reportlab.platypus import Paragraph
  8.  
  9. my_Style=ParagraphStyle('My Para style',
  10. fontName='Times-Roman',
  11. #backColor='#F1F1F1',
  12. fontSize=8,
  13. #borderColor='#FFFF00',
  14. #borderWidth=1,
  15. #borderPadding=(5,5,5),
  16. #leading=5,
  17. alignment=1
  18. )
  19.  
  20. packet = io.BytesIO()
  21. def coord(x, y, height, unit=1):
  22. x, y = x * unit, height - y * unit
  23. return x, y
  24. c = canvas.Canvas(packet, pagesize=A4)
  25. width, height = A4
  26.  
  27.  
  28. p1=Paragraph('''<b>blablalblaflasldflasd śćććśćśćśćśćśćśćś</b>''',my_Style)
  29. p1.wrapOn(c, (*coord(190, 25, height, mm)))
  30. p1.drawOn(c, (*coord(10, 172, height, mm)))
  31. c.showPage()
  32. c.save()
  33.  
  34. #move to the beginning of the StringIO buffer
  35. packet.seek(0)
  36.  
  37. # create a new PDF with Reportlab
  38. new_pdf = PdfFileReader(packet)
  39. # read your existing PDF
  40. existing_pdf = PdfFileReader(open("original.pdf", "rb"))
  41. output = PdfFileWriter()
  42. # add the "watermark" (which is the new pdf) on the existing page
  43. page = existing_pdf.getPage(0)
  44. page.mergePage(new_pdf.getPage(0))
  45. output.addPage(page)
  46. # finally, write "output" to a real file
  47. outputStream = open("destination30.pdf", "wb")
  48. output.write(outputStream)
  49. outputStream.close()
Advertisement
Add Comment
Please, Sign In to add comment