Guest User

Untitled

a guest
Dec 6th, 2022
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 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. from reportlab.pdfbase.pdfmetrics import registerFont
  9. from reportlab.pdfbase.ttfonts import TTFont
  10. registerFont(TTFont('Arial','ARIAL.ttf'))
  11.  
  12. my_Style=ParagraphStyle('My Para style',
  13. fontName='Arial',
  14. backColor='#F1F1F1',
  15. fontSize=8,
  16. borderColor='#FFFF00',
  17. borderWidth=1,
  18. borderPadding=(0,0,0),
  19. #leading=5,
  20. alignment=1
  21. )
  22.  
  23. my_Style2=ParagraphStyle('My Para style2',
  24. fontName='Arial',
  25. backColor='#F1F1F1',
  26. fontSize=8,
  27. borderColor='#FFFF00',
  28. borderWidth=1,
  29. borderPadding=(0,0,0),
  30. #leading=5,
  31. alignment=0
  32. )
  33. packet = io.BytesIO()
  34. def coord(x, y, height, unit=1):
  35. x, y = x * unit, height - y * unit
  36. return x, y
  37. c = canvas.Canvas(packet, pagesize=A4)
  38. width, height = A4
  39.  
  40.  
  41. p1=Paragraph('''PROJEKT BUDOWLANY ROZBUDOWY SIECI GAZOWEJ Ś/C WRAZ Z BUDOWĄ PRZYŁĄCZY GAZU DO PIĘCIU SZAFEK GAZOWYCH ZLOKALIZOWANYCH NA DZIAŁKACH NR 47, 454/2, 454/4, 42/2.
  42. ''',my_Style)
  43. p1.wrapOn(c, (*coord(190, 25, height, mm))) #prostokat
  44. p1.drawOn(c, (*coord(10, 175, height, mm))) #tekst
  45.  
  46. p2=Paragraph('''Hardego''',my_Style2)
  47. p2.wrapOn(c, (*coord(93, 10, height, mm))) #prosotkat
  48. p2.drawOn(c, (*coord(10, 135, height, mm))) #tekst
  49.  
  50. p3=Paragraph('''meijscowosc''',my_Style2)
  51. p3.wrapOn(c, (*coord(93, 10, height, mm))) #prosotkat
  52. p3.drawOn(c, (*coord(10, 146, height, mm))) #tekst
  53.  
  54. p4=Paragraph('''miejscowowsc2''',my_Style2)
  55. p4.wrapOn(c, (*coord(93, 10, height, mm))) #koordynaty prostokatya, szerokosc x wysokosc
  56. p4.drawOn(c, (*coord(108, 146, height, mm))) #koordynaty x i y od miejsca startu tesktu
  57.  
  58. p5=Paragraph('''123/4, 123/4, 123/4, 123/4, 123/4, 123/4, 123/4, 123/4,''',my_Style2)
  59. p5.wrapOn(c, (*coord(44, 30, height, mm))) #koordynaty prostokatya, szerokosc x wysokosc
  60. p5.drawOn(c, (*coord(158, 138, height, mm))) #koordynaty x i y od miejsca startu tesktu (y im wieksze tym nizej strony) x im mniejsze tym blizej lewej krawedzi
  61.  
  62. p6=Paragraph('''bla bla''',my_Style2)
  63. p6.wrapOn(c, (*coord(93, 10, height, mm))) #prosotkat
  64. p6.drawOn(c, (*coord(10, 241, height, mm))) #tekst
  65.  
  66. c.showPage()
  67. c.save()
  68.  
  69. #move to the beginning of the StringIO buffer
  70. packet.seek(0)
  71.  
  72. # create a new PDF with Reportlab
  73. new_pdf = PdfFileReader(packet)
  74. # read your existing PDF
  75. existing_pdf = PdfFileReader(open("original.pdf", "rb"))
  76. output = PdfFileWriter()
  77. # add the "watermark" (which is the new pdf) on the existing page
  78. page = existing_pdf.getPage(0)
  79. page.mergePage(new_pdf.getPage(0))
  80. output.addPage(page)
  81. # finally, write "output" to a real file
  82. outputStream = open("destination31.pdf", "wb")
  83. output.write(outputStream)
  84. outputStream.close()
  85.  
Advertisement
Add Comment
Please, Sign In to add comment