def html_to_pdf(source, export_dir, filename, original_url, use_print_css, extra_options=[]): # First we need to store the source in a temporary # file. html_filename = find_filename(export_dir, filename, 'html') if not html_filename: return None, ['no_filename_temp_html'] html_file = file('%s/%s' % (export_dir, html_filename), 'wb') html_file.write(source) html_file.close() # Run the wkhtmltopdf command. args = [wk_command, '--disable-javascript', 'file://%s/%s' % (export_dir, html_filename), '%s/%s' % (export_dir, filename)] if use_print_css: args.insert(2, '--print-media-type') for opt in extra_options: args.insert(2, opt) try: proc = subprocess.Popen(args, stdin=TemporaryFile(), stdout=TemporaryFile(), stderr=TemporaryFile()) timer = Timer(10, lambda p: p.kill, [proc]) timer.start() proc.communicate() timer.cancel() except: logger.error('Running wkhtmltopdf failed. Please check that ' 'you use a version compatible with your OS and ' 'the version is 0.9.') return None, ['pdf_generation_failed'] try: os.remove('%s/%s' % (export_dir, html_filename)) except IOError: logger.error('Temp file does not exist: %s/%s' % ( export_dir, html_filename)) try: pdf_file = file('%s/%s' % (export_dir, filename), 'r') except IOError: logger.error('No PDF output file') return None, ['pdf_generation_failed'] return pdf_file, None
def html_to_pdf(source, export_dir, filename, original_url, use_print_css, extra_options=[]): # First we need to store the source in a temporary # file. html_filename = find_filename(export_dir, filename, 'html') if not html_filename: return None, ['no_filename_temp_html'] html_file = file('%s/%s' % (export_dir, html_filename), 'wb') html_file.write(source) html_file.close() # Run the wkhtmltopdf command. args = [wk_command, '--disable-javascript', '--encoding', 'utf-8', 'file://%s/%s' % (export_dir, html_filename), '%s/%s' % (export_dir, filename)] if use_print_css: args.insert(4, '--print-media-type') for opt in extra_options: args.insert(4, opt) try: proc = subprocess.Popen(args, stdin=TemporaryFile(), stdout=TemporaryFile(), stderr=TemporaryFile()) if hasattr(proc, 'kill'): # Plone 4 timer = Timer(10, proc.kill) else: # Plone 3 timer = Timer(10, lambda pid: os.kill(pid, signal.SIGKILL), [proc.pid]) timer.start() proc.communicate() timer.cancel() except Exception, err: logger.error('Running wkhtmltopdf failed. wkhtmltopdf cmd: %s. ' 'Error: %s', args, err) return None, ['pdf_generation_failed']
def generate_temp_filename(self): """ Generates the filename used to store the PDF file. Basically the md5 hash of th user's email followed by a timestamp. If the user is anonymous, just use the timestamp. In case of conflict, we just happen '-x' at the end. """ prefix = self.generate_filename_prefix() now = datetime.now() # Ok that might not be the best timestamp system, but it's # enough for our needs. timestamp = ''.join([str(x) for x in now.timetuple()]) filename = prefix + timestamp return find_filename(self.tempdir, filename)
def html_to_pdf(source, export_dir, filename, original_url, use_print_css, extra_options=[]): # First we need to store the source in a temporary # file. html_filename = find_filename(export_dir, filename, "html") if not html_filename: return None, ["no_filename_temp_html"] try: source = source.encode("utf8") except UnicodeDecodeError: # When the source is sent through Ajax, it's already decoded # and causes an error here. pass html_file = file("%s/%s" % (export_dir, html_filename), "wb") html_file.write(source) html_file.close() # Run the wkhtmltopdf command. args = [ wk_command, "--disable-javascript", "file://%s/%s" % (export_dir, html_filename), "%s/%s" % (export_dir, filename), ] if use_print_css: args.insert(2, "--print-media-type") for opt in extra_options: args.insert(2, opt) try: p = subprocess.Popen(args) p.wait() except: logger.error( "Running wkhtmltopdf failed. Please check that " + "you use a version compatible with your OS and " + "the version is 0.9." ) return None, ["pdf_generation_failed"] os.remove("%s/%s" % (export_dir, html_filename)) pdf_file = file("%s/%s" % (export_dir, filename), "r") return pdf_file, None