2010-05-27 13 views
10

Jak umieścić obraz na istniejącym pliku PDF w określonej lokalizacji współrzędnych. Pdf przedstawia arkusz rysunku z jedną stroną. Obraz zostanie przeskalowany. Sprawdzam ReportLab, ale nie mogę znaleźć odpowiedzi. Dzięki.Umieść obraz nad plikiem PDF

+0

Czy obraz zawsze występuje w tym samym miejscu? –

+0

Tak, w zasadzie jest to znaczek w lewym dolnym rogu. –

+0

Czy odpowiedź poniżej jest wystarczająca? Jeśli nie, to co jest z nim nie tak? – theheadofabroom

Odpowiedz

11

http://pybrary.net/pyPdf/:

from pyPdf import PdfFileWriter, PdfFileReader 

output = PdfFileWriter() 
input1 = PdfFileReader(file("document1.pdf", "rb")) 
watermark = PdfFileReader(file("watermark.pdf", "rb")) 

page4.mergePage(watermark.getPage(0)) 

# finally, write "output" to document-output.pdf 
outputStream = file("document-output.pdf", "wb") 
output.write(outputStream) 
outputStream.close() 

myślę, że to jak watermark, zobacz instrukcję dla lepszego pomysłu

+0

Dzięki temu działało dobrze, po prostu oszukano metody PdfFileReader.getPage() i PdfFileWriter.addPage() z dokumentów. –

+0

Proszę zobaczyć mój komentarz powyżej. – Auston

+0

Wygląda na to, że PyPdf nie jest już utrzymywany, ani zapowiadana strona kontynuatora. Jakie inne rozwiązanie istnieje w dłuższej perspektywie? – lalebarde

-2

Ponieważ jego istniejący pdf, najbardziej prosty sposób, aby to zrobić jest:

  1. Konwertuj pdf na .doc lub .odt (Sprawdź http://www.zamzar.com/)
  2. Dodaj obrazy do przekonwertowanego pliku, ale chcesz .
  3. Konwersja z powrotem do formatu PDF (OpenOffice czy LibreOffice ułatwia zapisać PDF)

PS: Jeśli plik PDF musi dalej edytowane zawsze zachować kopię zapasową pliku .doc źródłowego, tak, że zmiany mogą być zrobione z łatwością, zbyt dużo konwersji ma zły wpływ na jakość plików.

6

Połączyłem ReportLab (http://www.reportlab.com/software/opensource/rl-toolkit/download/) i pyPDF (http://pybrary.net/pyPdf/), aby wstawić obraz bezpośrednio, nie mając do generowania PDF z góry:

from pyPdf import PdfFileWriter, PdfFileReader 
from reportlab.pdfgen import canvas 
from StringIO import StringIO 


# Using ReportLab to insert image into PDF 
imgTemp = StringIO() 
imgDoc = canvas.Canvas(imgTemp) 

# Draw image on Canvas and save PDF in buffer 
imgPath = "path/to/img.png" 
imgDoc.drawImage(imgPath, 399, 760, 160, 160) ## at (399,760) with size 160x160 
imgDoc.save() 

# Use PyPDF to merge the image-PDF into the template 
page = PdfFileReader(file("document.pdf","rb")).getPage(0) 
overlay = PdfFileReader(StringIO(imgTemp.getvalue())).getPage(0) 
page.mergePage(overlay) 

#Save the result 
output = PdfFileWriter() 
output.addPage(page) 
output.write(file("output.pdf","w")) 
19

To było 5 lat, myślę, że te odpowiedzi potrzebują trochę TLC. Oto kompletne rozwiązanie.

Poniżej jest testowany z Pythonem 2.7

Install Zależności

pip install reportlab 
pip install pypdf2 

Czy magiczne

from reportlab.pdfgen import canvas 
from PyPDF2 import PdfFileWriter, PdfFileReader 

# Create the watermark from an image 
c = canvas.Canvas('watermark.pdf') 

# Draw the image at x, y. I positioned the x,y to be where i like here 
c.drawImage('test.png', 15, 720) 

# Add some custom text for good measure 
c.drawString(15, 720,"Hello World") 
c.save() 

# Get the watermark file you just created 
watermark = PdfFileReader(open("watermark.pdf", "rb")) 

# Get our files ready 
output_file = PdfFileWriter() 
input_file = PdfFileReader(open("test2.pdf", "rb")) 

# Number of pages in input document 
page_count = input_file.getNumPages() 

# Go through all the input file pages to add a watermark to them 
for page_number in range(page_count): 
    print "Watermarking page {} of {}".format(page_number, page_count) 
    # merge the watermark with the page 
    input_page = input_file.getPage(page_number) 
    input_page.mergePage(watermark.getPage(0)) 
    # add page from input file to output document 
    output_file.addPage(input_page) 

# finally, write "output" to document-output.pdf 
with open("document-output.pdf", "wb") as outputStream: 
    output_file.write(outputStream) 

Referencje:

nowego domu z pypdf: http://mstamy2.github.io/PyPDF2/

ReportLab docs: http://www.reportlab.com/apis/reportlab/2.4/pdfgen.html

ReportLab kompletny User: https://www.reportlab.com/docs/reportlab-userguide.pdf

+2

Podczas gdy twoja odpowiedź jest aktualna, biblioteka [pdfrw] (https://github.com/pmaupin/pdfrw/) może również [znak wodny] (https://github.com/pmaupin/pdfrw/blob /master/examples/watermark.py) w bardzo podobny sposób, a także można przejść w drugą stronę - umożliwiając korzystanie z wcześniej istniejących plików PDF, tak jakby były obrazami (bez rasteryzacji) w plikach PDF, które [kompilujesz za pomocą reportlab ] (https://github.com/pmaupin/pdfrw/blob/master/examples/rl1/platypus_pdf_template.py). Zrzeczenie się: Jestem autorem pdfrw ... –

+0

Ah miło, będę o tym pamiętać –

0

To co pracował dla mnie

from PyPDF2 import PdfFileWriter, PdfFileReader 

def watermarks(temp, watermar,new_file): 
    template = PdfFileReader(open(temp, 'rb')) 
    wpdf = PdfFileReader(open(watermar, 'rb')) 
    watermark = wpdf.getPage(0) 

    for i in xrange(template.getNumPages()): 
     page = template.getPage(i) 
     page.mergePage(watermark) 
     output.addPage(page) 

     with open(new_file, 'wb') as f: 
      output.write(f) 
1

Thx do poprzednich odpowiedzi. Moja droga z python3.4

# -*- coding: utf-8 -*- 
from io import BytesIO 
from PyPDF2 import PdfFileWriter, PdfFileReader 
from reportlab.pdfgen import canvas 
from reportlab.lib.pagesizes import A4 

def gen_pdf(): 
    # there are 66 slides (1.jpg, 2.jpg, 3.jpg...) 
    path = 'slades/{0}.jpg' 
    pdf = PdfFileWriter() 

    for num in range(1, 67): # for each slide 
     # Using ReportLab Canvas to insert image into PDF 
     imgTemp = BytesIO() 
     imgDoc = canvas.Canvas(imgTemp, pagesize=A4) 
     # Draw image on Canvas and save PDF in buffer 
     imgDoc.drawImage(path.format(num), -25, -45) 
     # x, y - start position 
     # in my case -25, -45 needed 
     imgDoc.save() 
     # Use PyPDF to merge the image-PDF into the template 
     pdf.addPage(PdfFileReader(BytesIO(imgTemp.getvalue())).getPage(0)) 

    pdf.write(open("output.pdf","wb")) 


if __name__ == '__main__': 
    gen_pdf() 
Powiązane problemy