<?php //   exec('xvfb-run python2 html2pdf.py file:///tmp/in.html /tmp/out.pdf'); //  URL exec('xvfb-run python2 html2pdf.py http://habrahabr.ru /tmp/habr.pdf'); ?> [user@rdesk ~]$ xpra start :99
 <?php //   exec('DISPLAY=:99 python2 html2pdf.py file:///tmp/in.html /tmp/out.pdf'); //  URL exec('DISPLAY=:99 python2 html2pdf.py http://habrahabr.ru /tmp/habr.pdf'); ?>  #!/usr/bin/env python2 # -*- coding: UTF-8 -*- from PyQt4.QtCore import * from PyQt4.QtGui import * from PyQt4.QtWebKit import * import sys #     if len(sys.argv) != 3: print "USAGE app.py URL FILE" sys.exit() #   def html2pdf(f_url, f_name): #  QT  app = QApplication(sys.argv) #  "" web = QWebView() #  URL   web.load(QUrl(f_url)) #   printer = QPrinter() #   printer.setPageSize(QPrinter.A4) #   printer.setOutputFormat(QPrinter.PdfFormat) #    printer.setOutputFileName(f_name) #    ""  PDF def convertIt(): web.print_(printer) QApplication.exit() #    ,   ,   "" PDF QObject.connect(web, SIGNAL("loadFinished(bool)"), convertIt) sys.exit(app.exec_()) html2pdf(sys.argv[1], sys.argv[2]) 
 #!/usr/bin/env python2 # -*- coding: UTF-8 -*- import pika, os, time, threading, logging, redis, Queue RBT_HOST = 'rabbit.myhost.ru' RBT_QE = 'pdf.render' RDS_HOST = 'redis.myhost.ru' LOG = 'watcher.log' MAX_THREADS = 4 #    logging.basicConfig(level=logging.DEBUG, format='%(asctime)-15s - %(threadName)-10s - %(message)s', filename=LOG ) def render(msg_id): #     output_file = '/tmp/' + msg_id + '.pdf' input_file = '/tmp/' + msg_id + '.html' #  HTML   logging.debug('[R] Loading HTML from DB...') dbcon_r = redis.Redis(RDS_HOST, port=6379, db=0) bq = dbcon_r.get(msg_id) logging.debug('[R] HTML loaded...') #  HTML    logging.debug('[R] Write tmp HTML...') fin1 = open(input_file, "wb") fin1.write(bq) fin1.close() logging.debug('[R] HTML writed...') #     command = 'DISPLAY=:99 python2 ./html2pdf.py %s %s' % ( 'file://' + input_file, output_file ) #    t_start = time.time() sys_output = int(os.system(command)) t_finish = time.time() #       i_size = str(os.path.getsize(input_file)/1024) o_size = str(os.path.getsize(output_file)/1024) #      log dbg_mesg = '[R] Render [msg.id:' + msg_id + '] ' +\ '[rend.time:' + str(t_finish-t_start) + 'sec]' + \ '[in.fle:' + input_file + '(' + i_size+ 'kb)]' +\ '[ou.fle:' + output_file + '(' + o_size + 'kb)]' #  log logging.debug(dbg_mesg) #  PDF logging.debug('[R] Loading PDF...') fin = open(output_file, "rb") binary_data = fin.read() fin.close() logging.debug('[R] PDF loaded...') #  ID   Q_  R_ msg_out = msg_id.split('_') msg = 'R_' + msg_out[1] + '_' + msg_out[2] #  PDF   logging.debug('[R] Write PDF 2 DB...') dbcon_r.set(msg, binary_data) logging.debug('[R] PDF commited...') #  ( ,   ) logging.debug('[R] DEL db record: ' + msg_id) dbcon_r.delete(msg_id) logging.debug('[R] DEL tmp: ' + output_file) os.remove(output_file) logging.debug('[R] DEL tmp: ' + input_file) os.remove(input_file) logging.debug('[R] Render done') # rets if not sys_output: return True, output_file return False, sys_output def catcher(q): '''   N     ''' while True: try: item = q.get() #     except Queue.Empty: break logging.debug('Queue send task to render: ' + item) render(item) #    q.task_done() #   #  logging.debug('Daemon START') #   TQ = Queue.Queue() logging.debug('Starting threads...') #    for i in xrange(MAX_THREADS): wrkr_T = threading.Thread(target = catcher, args=(TQ,)) wrkr_T.daemon = True wrkr_T.start() logging.debug('Thread: ' + str(i) + ' started') logging.debug('Start Consuming...') #  ,  ,    try: connection = pika.BlockingConnection(pika.ConnectionParameters(host = RBT_HOST)) channel = connection.channel() channel.queue_declare(queue = RBT_QE) def callback(ch, method, properties, body): TQ.put(body) logging.debug('Consumer got task: ' + body) channel.basic_consume(callback, queue = RBT_QE, no_ack = True) channel.start_consuming() except KeyboardInterrupt: logging.debug('Daemon END') print '\nApp terminated!' Source: https://habr.com/ru/post/128078/
All Articles