ในการสร้างไฟล์ E-book PDF ด้วย PySide บน Python เราต้องใช้ประกอบของ Qt ต่อไปนี้
- QtCore (โค้ดหลักของ Qt)
- QtGui (ใช้แสดงหน้าต่าง GUI)
- QtWebKit ใช้สำหรับดึดหน้าเว็บหรือโค้ด HTML แล้วส่งออกเป็น PDF
import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtWebKit import *
app = QApplication(sys.argv)
web = QWebView()
web.load(QUrl("https://python3.wannaphong.com")) #ดึงหน้าเว็บมา
#web.show()
printerpdk = QPrinter()
printerpdk.setPageSize(QPrinter.A4) #รูปแบบ PDF เป็น A4
printerpdk.setOutputFormat(QPrinter.PdfFormat)
printerpdk.setOutputFileName("file1.pdf") #กำหนดส่งออกเป็นไฟล์ file1.pdf
def convertIt():
web.print_(printerpdk)
print("สร้าง Pdf แล้ว")
QApplication.exit()
QObject.connect(web, SIGNAL("loadFinished(bool)"),
convertIt)
sys.exit(app.exec_())
ผลลัพธ์ไฟล์ file1.pdfติดตามบทความต่อไปนะครับ
ขอบคุณครับ

0 ความคิดเห็น:
แสดงความคิดเห็น
แสดงความคิดเห็นได้ครับ :)