我正在寻找使用Python将网页打印成本地文件PDF的解决方案.一个很好的解决方案是使用Qt,在这里可以找到https://bharatikunal.wordpress.com/2010/01/.

它一开始不起作用,因为我在安装PyQt4时遇到了问题,因为它会给出错误消息,比如"ImportError: No module named PyQt4.QtCore"和"ImportError: No module named PyQt4.QtCore".

这是因为PyQt4没有正确安装. 我过go 的库位于C:\Python27\Lib,但是它不是针对PyQt4的.

实际上,它只需要从http://www.riverbankcomputing.com/software/pyqt/download下载(注意您正在使用的正确的Python版本),并将其安装到C:\Python27(我的 case ).就这样.

现在脚本运行得很好,所以我想分享它.有关使用QPRINTER的更多选项,请参阅http://qt-project.org/doc/qt-4.8/qprinter.html#Orientation-enum.

推荐答案

感谢下面的帖子,我能够在网页上添加链接地址来打印和呈现在PDF上生成的时间,无论它有多少页.

Add text to Existing PDF using Python

https://github.com/disflux/django-mtr/blob/master/pdfgen/doc_overlay.py

要按如下方式共享脚本:

import time
from pyPdf import PdfFileWriter, PdfFileReader
import StringIO
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from xhtml2pdf import pisa
import 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 given
web.load(QUrl(url))
printer = QPrinter()
#setting format
printer.setPageSize(QPrinter.A4)
printer.setOrientation(QPrinter.Landscape)
printer.setOutputFormat(QPrinter.PdfFormat)
#export file as c:\tem_pdf.pdf
printer.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 generated

outputPDF = PdfFileWriter()
packet = StringIO.StringIO()
# create a new PDF with Reportlab
can = canvas.Canvas(packet, pagesize=letter)
can.setFont("Helvetica", 9)
# Writting the new line
oknow = 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 buffer
packet.seek(0)
new_pdf = PdfFileReader(packet)
# read your existing PDF
existing_pdf = PdfFileReader(file(tem_pdf, "rb"))
pages = existing_pdf.getNumPages()
output = PdfFileWriter()
# add the "watermark" (which is the new pdf) on the existing page
for 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 file
outputStream = file(final_file, "wb")
output.write(outputStream)
outputStream.close()

print final_file, 'is ready.'

Html相关问答推荐

将容器水平偏离中心放置在桌面上;在移动设备上平稳地集中缩小规模

如何才能完成这个背景按钮

restrict. form—text width to input s width'

如何使我的组织 struct 图的连接线响应?

为所有必填字段添加所需的占位符文本(Angular Material)

使用无限数量的元素创建特定的CSS网格

用于删除页面页眉上的超链接的CSS

窗口视图之外的下拉菜单位置

Django HTML标记-Merge for Loop with Conditional语句

div居中碰撞问题

有没有一种方法可以提高代码中tailwind 类名的可读性?

有没有办法移动占位符?

使用简单的 HTML 设计公司徽标和文本

为 HTML5 文本字段设置最后六位正则表达式模式

如何使用CSS Select 表亲元素

如何使用 CSS Flex 设计一个导航栏,其中一组项目左对齐而另一组项目右对齐?

如何在 blazor 中单击单个按钮调用 2 个等待任务

涉及短代码时如何在页面上定位元素?

html tailwindcss 给 li 标签添加边框

如何使用 :hover zoom 重叠图像?