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
- from reportlab.pdfbase.pdfmetrics import registerFont
- from reportlab.pdfbase.ttfonts import TTFont
- registerFont(TTFont('Arial','ARIAL.ttf'))
- my_Style=ParagraphStyle('My Para style',
- fontName='Arial',
- backColor='#F1F1F1',
- fontSize=8,
- borderColor='#FFFF00',
- borderWidth=1,
- borderPadding=(0,0,0),
- #leading=5,
- alignment=1
- )
- my_Style2=ParagraphStyle('My Para style2',
- fontName='Arial',
- backColor='#F1F1F1',
- fontSize=8,
- borderColor='#FFFF00',
- borderWidth=1,
- borderPadding=(0,0,0),
- #leading=5,
- alignment=0
- )
- 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('''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.
- ''',my_Style)
- p1.wrapOn(c, (*coord(190, 25, height, mm))) #prostokat
- p1.drawOn(c, (*coord(10, 175, height, mm))) #tekst
- p2=Paragraph('''Hardego''',my_Style2)
- p2.wrapOn(c, (*coord(93, 10, height, mm))) #prosotkat
- p2.drawOn(c, (*coord(10, 135, height, mm))) #tekst
- p3=Paragraph('''meijscowosc''',my_Style2)
- p3.wrapOn(c, (*coord(93, 10, height, mm))) #prosotkat
- p3.drawOn(c, (*coord(10, 146, height, mm))) #tekst
- p4=Paragraph('''miejscowowsc2''',my_Style2)
- p4.wrapOn(c, (*coord(93, 10, height, mm))) #koordynaty prostokatya, szerokosc x wysokosc
- p4.drawOn(c, (*coord(108, 146, height, mm))) #koordynaty x i y od miejsca startu tesktu
- p5=Paragraph('''123/4, 123/4, 123/4, 123/4, 123/4, 123/4, 123/4, 123/4,''',my_Style2)
- p5.wrapOn(c, (*coord(44, 30, height, mm))) #koordynaty prostokatya, szerokosc x wysokosc
- 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
- p6=Paragraph('''bla bla''',my_Style2)
- p6.wrapOn(c, (*coord(93, 10, height, mm))) #prosotkat
- p6.drawOn(c, (*coord(10, 241, height, mm))) #tekst
- 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("destination31.pdf", "wb")
- output.write(outputStream)
- outputStream.close()
Advertisement
Add Comment
Please, Sign In to add comment