Convert webpage to images or PDF using Pyside QtWebkit python bindings
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

webpage2img.py 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/python3
  2. #
  3. # Copyright 2017 Weber Yann
  4. #
  5. # This file is part of webpage2img.
  6. #
  7. # webpage2img is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # any later version.
  11. #
  12. # webpage2img is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with webpage2img. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. import sys
  21. import argparse
  22. from PySide import QtWebKit, QtCore, QtGui
  23. #self._app = QtGui.QApplication(sys.argv)
  24. class WebPage2Img(object):
  25. def __init__(self, uri):
  26. self._out_fname = None
  27. self.uri = uri
  28. self._app = QtGui.QApplication(sys.argv)
  29. #After this point we are fucked up because the app catch
  30. #all exceptions :/
  31. self.wxview = QtWebKit.QWebView()
  32. def png(self, filename):
  33. self.__output(filename, self._png)
  34. def pdf(self, filename):
  35. self.__output(filename, self._pdf)
  36. def __output(self, filename, slot):
  37. self._out_fname = filename
  38. self.wxview.loadFinished.connect(slot)
  39. print('PNG rendering to : %s' % self._out_fname)
  40. self.__load()
  41. ##@brief PNG rendering function
  42. #@param ok : no idea, sent by connect
  43. #@utils.ExitingExptSlot(self._app, "bool")
  44. def _png(self, ok):
  45. print('_png')
  46. self.wxview.page().setViewportSize(self.wxview.page().currentFrame().contentsSize())
  47. image = QtGui.QImage(self.wxview.page().viewportSize(),QtGui.QImage.Format_ARGB32)
  48. painter = QtGui.QPainter(image)
  49. self.wxview.page().mainFrame().render(painter)
  50. painter.end()
  51. image.save(self._out_fname)
  52. self.exit()
  53. def _pdf(self, ok):
  54. print('_pdf')
  55. printer = QtGui.QPrinter()
  56. printer.setOutputFileName(self._out_fname)
  57. printer.setOutputFormat(QtGui.QPrinter.PdfFormat)
  58. printer.setPageSize(QtGui.QPrinter.A4)
  59. printer.setOrientation(QtGui.QPrinter.Landscape)
  60. #printer.setFullPage(True)
  61. self.wxview.page().mainFrame().print_(printer)
  62. self.exit()
  63. def exit(self, retcode = 0):
  64. print('Exiting.')
  65. self._app.exit(retcode)
  66. exit(retcode)
  67. def __load(self):
  68. if self._out_fname is None:
  69. raise Exception('Do not run if no out set')
  70. self.wxview.load(QtCore.QUrl(self.uri))
  71. sys.exit(self._app.exec_())
  72. if __name__ == '__main__':
  73. parser = argparse.ArgumentParser(description='Convert a webpage to an \
  74. image.')
  75. parser.add_argument('-f', '--format', dest='fmt', metavar='pdf|png', type=str,
  76. default='pdf', help='Choose output format')
  77. parser.add_argument('-o', '--output', metavar='FILENAME', type=str,
  78. default='out.pdf', help='Output file')
  79. parser.add_argument('uri', metavar='URI', type=str)
  80. args = parser.parse_args()
  81. wp = WebPage2Img(args.uri)
  82. if args.fmt.lower() == 'png':
  83. wp.png(args.output)
  84. elif args.fmt.lower() == 'pdf':
  85. wp.pdf(args.output)
  86. else:
  87. print('Unknown format "%s"\n' % args.fmt)
  88. parser.print_help()
  89. wp.exit(1)