def save_pdf(html, filename): """ 把所有html文件保存到pdf文件 :param html: html内容 :param file_name: pdf文件名 :return: """ options = { 'page-size': 'Letter', 'margin-top': '0.75in', 'margin-right': '0.75in', 'margin-bottom': '0.75in', 'margin-left': '0.75in', 'encoding': "UTF-8", 'custom-header': [('Accept-Encoding', 'gzip')], 'cookie': [ ('cookie-name1', 'cookie-value1'), ('cookie-name2', 'cookie-value2'), ], 'outline-depth': 10, } pdfkit.from_string(html, filename, options=options)
def handle_request(request, relative_path, directory_settings): """ Handles the PDF requests :param request: :param relative_path: :param directory_settings: :return: """ mdexport = MarkdownExport(request) specific_filetype = None if 'specific' in dict(request.params): specific_filetype = request.params['specific'] output = mdexport.export_folder(relative_path, directory_settings, specific_filetype) html_text = markdown(output, output_format='html4') # TODO: fix this Path problem config = pdfkit.configuration(wkhtmltopdf='C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe') # TODO: replace by mkdtemp if not os.path.exists(relative_path + '/.temp/'): os.mkdir(relative_path + '/.temp/') pdfkit.from_string(html_text, relative_path + '/.temp/report.pdf', configuration=config) with open(relative_path + '/.temp/report.pdf', 'rb') as report: return Response(body=report.read(), content_type='application/pdf')
def doBingo(inputfile, outputfile, title, freespace_text): # Read input file and select 24 random items bingoItems = readFile(inputfile) if (len(bingoItems) < 24): print 'Too few items in input file. You need at least 24.' selected_items = random.sample(bingoItems, 24) # Create the HTML for the bingo card bingo_html = createBingoHtml(title, selected_items, freespace_text) # Define settings for pdfkit options = { 'page-size': 'Letter', 'margin-top': '0.75in', 'margin-right': '0.75in', 'margin-bottom': '0.75in', 'margin-left': '0.75in', 'encoding': "UTF-8", } # Create pdf from the HTML string pdfkit.from_string(bingo_html, outputfile, options=options)