感谢下面的帖子,无论页面有多少页,我都可以添加要打印的网页链接地址并在生成的PDF上显示时间。
使用Python将文本添加到现有PDF
https://github.com/disflux/django-
mtr/blob/master/pdfgen/doc_overlay.py
要共享脚本,如下所示:
import timefrom pyPdf import PdfFileWriter, PdfFileReaderimport StringIOfrom reportlab.pdfgen import canvasfrom reportlab.lib.pagesizes import letterfrom xhtml2pdf import pisaimport sys from PyQt4.QtCore import *from PyQt4.QtGui import * from PyQt4.QtWebKit import *url = 'http://www.yahoo.com'tem_pdf = "c:\tem_pdf.pdf"final_file = "c:\younameit.pdf"app = QApplication(sys.argv)web = QWebView()#Read the URL givenweb.load(QUrl(url))printer = QPrinter()#setting formatprinter.setPageSize(QPrinter.A4)printer.setOrientation(QPrinter.Landscape)printer.setOutputFormat(QPrinter.PdfFormat)#export file as c:tem_pdf.pdfprinter.setOutputFileName(tem_pdf)def convertIt(): web.print_(printer) QApplication.exit()QObject.connect(web, SIGNAL("loadFinished(bool)"), convertIt)app.exec_()sys.exit# Below is to add on the weblink as text and present date&time on PDF generatedoutputPDF = PdfFileWriter()packet = StringIO.StringIO()# create a new PDF with Reportlabcan = canvas.Canvas(packet, pagesize=letter)can.setFont("Helvetica", 9)# Writting the new lineoknow = time.strftime("%a, %d %b %Y %H:%M")can.drawString(5, 2, url)can.drawString(605, 2, oknow)can.save()#move to the beginning of the StringIO bufferpacket.seek(0)new_pdf = PdfFileReader(packet)# read your existing PDFexisting_pdf = PdfFileReader(file(tem_pdf, "rb"))pages = existing_pdf.getNumPages()output = PdfFileWriter()# add the "watermark" (which is the new pdf) on the existing pagefor x in range(0,pages): page = existing_pdf.getPage(x) page.mergePage(new_pdf.getPage(0)) output.addPage(page)# finally, write "output" to a real fileoutputStream = file(final_file, "wb")output.write(outputStream)outputStream.close()print final_file, 'is ready.'


