def generate_pdf(request, site): reportPdfUrl = 'http://%s/report/view/%s' % (request.META['HTTP_HOST'],str(site.pk)) outputStream = BytesIO() reportPdfFile = '%s/download_%s.pdf' % (settings.DOWNLOAD_ROOT, site.pk) wkhtmltopdfBinLocationString = '/usr/local/bin/wkhtmltopdf' wkhtmltopdfBinLocationBytes = wkhtmltopdfBinLocationString.encode('utf-8') config = pdfkit.configuration(wkhtmltopdf=wkhtmltopdfBinLocationBytes) pdfkit.from_url(reportPdfUrl, reportPdfFile, configuration=config, options={ 'javascript-delay': 1500, 'load-error-handling': 'ignore' }) new_pdf = PdfFileMerger() new_pdf.append('%s/download_%s.pdf' % (settings.DOWNLOAD_ROOT, site.pk)) # finally, return output new_pdf.write(outputStream) final = outputStream.getvalue() outputStream.close() os.remove(reportPdfFile) return final
def save_answer(self, url, filename, save_as): if save_as == self.SAVE_AS_HTML: page_html = urllib2.urlopen(url).read() with open(filename, 'wb') as f: f.write(page_html) elif save_as == self.SAVE_AS_PDF: pdfkit.from_url(url, filename)
def fun(tag): global lol print 'Making pdf:', tag[0] uid = uuid.uuid4() pdfkit.from_url(tag[1], '.\\store\\'+str(uid)+'.pdf') lol.append((uid, tag[0])) return lol[-1]
def get_list_by_ponente_save_as_title(ponente, year): if not os.path.exists(ponente): os.makedirs(ponente) if not os.path.exists(ponente + '/' + year): os.makedirs(ponente + '/' + year) csvfile = year + ".csv" with open(csvfile, 'r') as csvFile: reader = csv.DictReader(csvFile, fieldnames=fieldnames_list) for row in reader: print row['ponente'] print row['year'] print row['month'] print row['day'] print text_util.slugify(row['title'].replace('\n', ' ')) if row['ponente'].find(ponente) is not -1: linkfriendly = row['link'].replace(DOC_VIEW, PDF_FRIENDLY) pdf_filename = ponente + '/' + year + '/' + text_util.slugify(row['title']) + '(' + year + ')' pdf_filename.replace('\n', ' ') month_count = 0 while os.path.isfile(pdf_filename + '.pdf'): month_count += 1 pdf_filename = ponente + '/' + year + '/' + text_util.slugify(row['title']) + '(' + str(month_count) + ')' + '(' + year + ')' pdf_filename.replace('\n', ' ') pdfkit.from_url(linkfriendly, pdf_filename + '.pdf') print pdf_filename + ' saved!'
def exportHtmlToPdf(results, folder): if not os.path.exists(folder): os.makedirs(folder) options = { 'page-size': 'Letter', 'margin-top': '0.75in', 'margin-right': '0.75in', 'margin-bottom': '0.75in', 'margin-left': '0.75in', 'encoding': 'UTF-8', # 'quiet': '' } # options = { # 'page-size': 'Letter', # 'margin-top': '0.75in', # 'margin-right': '0.75in', # 'margin-bottom': '0.75in', # 'margin-left': '0.75in', # 'encoding': "UTF-8", # 'quiet': '' # turn off wkhtmltopdf output # } for key, result in enumerate(results): title = result[0] url = result[2] localFileName = str(key+1) + '.pdf' print(title) print(url) print(localFileName) pdfkit.from_url(url, localFileName, options=options)
def get(self, request, format=None): name = request.GET.get("name", False) link = settings.ROOT_URL + "sales_report/?print=true" now = datetime.datetime.now().strftime("_%Y-%m-%d_%H:%M:%S") name = name + now pdfkit.from_url(link, "static/user_data/reports/" + name + ".pdf") return redirect("../static/user_data/reports/" + name + ".pdf")
def to_pdf(self, options={}, verbose=True): """Generates a pdf Parameters ---------- options : dict options are pdfkit.from_url options. See https://pypi.python.org/pypi/pdfkit verbose : bool iff True, gives output regarding pdf creation Returns ------- Path of generated pdf """ if verbose: print 'Generating report...' with open(self.__html_src_path, 'w') as html_out: html_out.write(self.__get_header()) html_out.write('\n'.join(self.__objects)) html_out.write(self.__get_footer()) if not verbose: options['quiet'] = '' pdfkit.from_url(self.__html_src_path, self.__report_path, options=options) report_path = self.get_report_path() if verbose: print 'Report written to {}'.format(report_path) return report_path
def get_list_by_ponente(ponente, year): if not os.path.exists(ponente): os.makedirs(ponente) if not os.path.exists(ponente + '/' + year): os.makedirs(ponente + '/' + year) csvfile = year + ".csv" with open(csvfile, 'r') as csvFile: reader = csv.DictReader(csvFile, fieldnames=fieldnames_list) for row in reader: print row['ponente'] print row['year'] print row['month'] print row['day'] if row['ponente'].find(ponente) is not -1: linkfriendly = row['link'].replace(DOC_VIEW, PDF_FRIENDLY) month = row['month'] day = row['day'] pdf_filename = ponente + '/' + year + '/' + month + '_' + day month_count = 0 while os.path.isfile(pdf_filename + '.pdf'): month_count += 1 pdf_filename = ponente + '/' + year + '/' + month + '_' + day + '_' + str(month_count) pdfkit.from_url(linkfriendly, pdf_filename + '.pdf') print pdf_filename + 'saved!'
def com_pdf_create(pdf_from, pdf_data, pdf_out_file): if pdf_from == 'url': pdfkit.from_url(pdf_data, pdf_out_file) elif pdf_from == 'file': pdfkit.from_file(pdf_data, pdf_out_file) else: pdfkit.from_string(pdf_data, pdf_out_file)
def tagpdf(pathac="",num=1,urll=''): request = urllib.request.Request("http://www.codeforces.com/api/problemset.problems?tags="+urll) response = urllib.request.urlopen(request) encoding = response.info().get_content_charset('utf8') data = json.loads(response.read().decode(encoding)) if data['status'] != 'OK': print(' ERROR\n') return ss = str(datetime.datetime.now().now()) path = pathac if not pathac: os.chdir(os.path.dirname(__file__)) path = os.getcwd() path = path + '/' + ss os.makedirs(path,exist_ok=True) path = path + '/' ''' print(path) ''' for i in range(0,num): if not data['result']['problems'][i]: break; a = "http://www.codeforces.com/problemset/problem/" a = a + str(data['result']['problems'][i]['contestId']) a = a + "/" a = a + str(data['result']['problems'][i]['index']) pdfkit.from_url(a,path + str(data['result']['problems'][i]['contestId'])+ str(data['result']['problems'][i]['index']))
def download_html_to_pdf(url,fname,location): if(url and fname): print "Save "+fname+" from "+url+" to "+location pdfkit.from_url(url,location+fname) print "Completed" else: print "Undefined url or file name"
def cetakURL(strPaperSize, strOrientation, strURL, strBranchCode, strGenre, strSubGenre, strBranchGenre): options = {'page-size': strPaperSize, 'orientation':strOrientation} strNamaFile = strGenre.lower()+"-"+strSubGenre + "-" + strBranchGenre +"-"+ strBranchCode +time.strftime("-%Y%m%d") + ".pdf" strNamaFile = prepareDirectory('OUTPUT', 'LOAN') + strNamaFile pdfkit.from_url(strURL, strNamaFile, options) return strNamaFile
def savepdf(url,title,category,annid): title = re.sub('[^0-9a-zA-Z]+', '-', title) filemane = 'tmp/' + annid +'_' + category + '_' + title + '.pdf' pdfkit.from_url(url, filemane) file = open(filemane, 'rb') gridfsid = fs.put( file.read(), filename=filemane) return gridfsid
def wiki2pdf(self, url, path=None, font='Serif'): if path is None: path = self.output_path m = hashlib.md5() m.update(url.encode("utf-8")) filename = m.hexdigest()[0:5]+".pdf" filepath = os.path.join(path, filename) pdfkit.from_url(url, filepath) return filename
def generate_html_report(self): """ Descript. : """ html_filename = os.path.join(\ self.test_directory, self.test_filename + ".html") pdf_filename = os.path.join(\ self.test_directory, self.test_filename + ".pdf") archive_filename = os.path.join(\ self.test_directory, datetime.now().strftime("%Y_%m_%d_%H") + "_" + \ self.test_filename) try: output_file = open(html_filename, "w") output_file.write(SimpleHTML.create_html_start("Beamline test summary")) output_file.write("<h1>Beamline %s Test results</h1>" % self.beamline_name) output_file.write("<h2>Executed tests:</h2>") table_cells = [] for test in self.results_list: table_cells.append(["bgcolor=%s" % TEST_COLORS_TABLE[test["result_bit"]], "<a href=#%s>%s</a>" % (test["short_name"], test["full_name"]), test["result_short"], test["start_time"], test["end_time"]]) table_rec = SimpleHTML.create_table(\ ["Name", "Result", "Start time", "End time"], table_cells) for row in table_rec: output_file.write(row) output_file.write("\n<hr>\n") for test_result in self.results_html_list: output_file.write(test_result + "\n") output_file.write(SimpleHTML.create_html_end()) output_file.close() self.emit("htmlGenerated", html_filename) logging.getLogger("HWR").info(\ "BeamlineTest: Test result written in file %s" % html_filename) except: logging.getLogger("HWR").error(\ "BeamlineTest: Unable to generate html report file %s" % html_filename) try: pdfkit.from_url(html_filename, pdf_filename) logging.getLogger("GUI").info("PDF report %s generated" % pdf_filename) except: logging.getLogger("GUI").info("Unable to generate PDF report %s" % pdf_filename) self.emit('testFinished', html_filename)
def __generate_pdf(url, output_path): options = { 'page-size': 'A4', 'margin-top': '1.27cm', 'margin-right': '1.27cm', 'margin-bottom': '1.27cm', 'margin-left': '1.27cm', 'encoding': "UTF-8", } pdfkit.from_url(url, output_path, options)
def convertToPDF(page_links, type): link_count=1 for i in page_links: file_loc="/home/vivek/Downloads/GeeksforGeeks/"+type+"/" file_name=type+str(link_count)+".pdf" try: pdfkit.from_url(str(i), file_loc+file_name) except: print("Some conversion error in "+ str(i)) link_count=link_count+1
def downloadAsPDF(self): links = self.getItems() for link in links: name = str(link.text) + '.pdf' joinedURL = self.domain + link.get('href') print joinedURL try: pdfkit.from_url(joinedURL, name) print 'success!' except: print 'error...'
def download_problem(problem_number): """Downloads the problem into the file `problem_number`.pdf, where `problem_number` is replaced by the actual number. :param problem_number: number of the problem. :type problem_number: str. """ id_ = _get_problem_id(problem_number) page = requests.get(constants.PROBLEM_LINK % id_) soup = BeautifulSoup(page.text) problem_link = soup.find('iframe').get('src', '') pdfkit.from_url(problem_link)
def post(self, request, format=None): name=request.POST.get('name', False) to_email=request.POST.get('to_email', False) subject=request.POST.get('subject', False) link= settings.ROOT_URL+'sales_report/?print=true' now = datetime.datetime.now().strftime("_%Y-%m-%d_%H:%M:%S") name=name+now pdfkit.from_url(link, 'static/user_data/reports/'+name+'.pdf') email = EmailMessage(subject, 'pfa, sales report generated on '+now, '*****@*****.**',[to_email],headers = {'Reply-To': '*****@*****.**'}) email.attach_file('static/user_data/reports/'+name+'.pdf') email.send("fail_silently=False") return Response('Mail successfully sent')
def pdf(type, data): current_dir = os.getcwd() folder = os.path.join(current_dir, type) if not os.path.exists(folder): os.mkdir(folder) for problem_name in data[type]: link = data[type][problem_name] pdf_name = problem_name + ".pdf" try: pdfkit.from_url(link, os.path.join(folder, pdf_name), configuration=config) except: pass
def POST(self): data = web.data() temp = [x for x in data.split("&")] temp.sort() try: url = temp[1].split("=")[1] name = temp[0].split("=")[1] urld = unquote(url).decode('utf8') filename = "%s.pdf" % name pdfkit.from_url(str(urld), filename) except Exception as e: pdfkit.from_string(str(e), 'error_%s.pdf' % name) return "sucess"
def generate_pdf(self, report_id): url = '%s/questionnaire_view_report_html?report_id=%s' % ( self.absolute_url(), report_id) http_cookies = self.REQUEST.environ['HTTP_COOKIE'] for cookie in http_cookies.split('; '): if cookie.startswith('__ac'): __ac = cookie.replace('__ac="', '')[:-1] options = {'print-media-type': False, 'no-images': False, 'cookie': '__ac=%s' % __ac} return pdfkit.from_url(url, False, options=options) else: options = {'print-media-type': False, 'no-images': False} return pdfkit.from_url(url, False, options=options)
def create_pdf(cls, url=None, filename=None, target=None, options=None): if options is None: options = cls.pdfkit_base_options() if target is None: target = settings.MEDIA_ROOT if filename is None: filename = 'pdf-file' path = '{}/{}.pdf'.format(target, filename) full_url = '{}{}'.format(settings.BASE_URL, url) print full_url pdfkit.from_url(full_url, path, options=options)
def enviar_correo_cancelacion_reserva_alojamiento(cls, host, reserva_id): # 1 - Generar el PDF que se enviará adjunto por correo reserva = Reserva.objects.detalles_reserva(id=reserva_id) usuario = reserva.usuario # Características y configuración del PDF options = {'page-size': 'A4', 'encoding': 'UTF-8'} config = pdfkit.configuration(wkhtmltopdf=globals.WKH2P_PATH) # Se general el PDF pdfkit.from_url(host + '/emails/pdf_cancelacion_reserva_alojamiento/%s' % (reserva_id), 'media/comprobantes_cancelacion_reserva/%s_C.pdf' % (reserva.codigo_reserva), configuration=config, options=options) # 2 - Crear el Email, adjuntar el PDF y enviarlo a la dirección del usuario que ha realizado la reserva # Aquí se definen todas las particularidades del Email que se quiere enviar asunto = 'Detalles de Cancelación de Reserva' remitente = '*****@*****.**' context = { 'username': usuario.user.username, } destinatarios = [ usuario.user.email, ] plantilla = 'emails/plantillas/cancelacion_reserva.html' imagenes = [ 'emails/templates/emails/imagenes_emails/logotipo_ontraveline.png', ] ficheros_adjuntos = [ 'media/comprobantes_cancelacion_reserva/%s_C.pdf' % (reserva.codigo_reserva), ] # Se llama a una función genérica que envía el email cls.enviar_correo( asunto=asunto, destinatarios=destinatarios, remitente=remitente, context=context, plantilla=plantilla, imagenes=imagenes, ficheros_adjuntos=ficheros_adjuntos, )
def company_getsanfangxieyi(request, student_id): print("test") company_id = request.session.get('u_id') if company_id: url1 = 'http://127.0.0.1:8000/company/sanfangxieyi_' + str( student_id) + '/?test=' + str(company_id) #url1 = 'http://127.0.0.1:8000/company/sanfangxieyi_235373/' url2 = 'static/sanfangxieyi/sanfangxieyi_' + str(student_id) + '.pdf' print(url1) print(url2) pdfkit.from_url(url1, url2) url3 = '/static/sanfangxieyi/sanfangxieyi_' + str(student_id) + '.pdf' return redirect(url3) else: redirect('/company/login/')
def downloadPortfolioPDF(request): filename = "portfolio.pdf" print("###################downloadPortfolioPDF#################") path_wkthmltopdf = "C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe" #urlhtml="C:\\wealthmanagement_data\\html\\Wealth_View.html" config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf) pdfkit.from_url("http://127.0.0.1:7000/screener/portfolioPDF/", filename, configuration=config) print("###################PDF#################") pdfDownload = open(filename, 'rb').read() #os.remove(filename) response = HttpResponse(pdfDownload, content_type="application/pdf") response['Content-Disposition'] = 'attachment; filename="portfolio.pdf"' return response
def order_paid_pdf(): """ represents paid off order list generate pdf processing url """ try: DIR = getcwd() + "/asset/" FILE = "paidoff.pdf" pdfkit.from_url(url_for("dashboard.order_paid_table", _external=True), DIR + FILE) except Exception: flash("error", "terjadi kesalahan saat membuat file pdf") return redirect(url_for("dashboard.order_paid")) else: return send_from_directory(DIR, FILE, as_attachment=True)
def send_message(): message = request.args.get('title') stock_link = request.args.get('stock_list') url = '/Users/casper.local/Desktop/Dev/Scrapper/webApp/static/stock_list/' + message + '.pdf' pdfkit.from_url(stock_link, url) #Add your own Bot TOKen bot_token = '1598350721:AAFA4YMJBqxeVfzIjfMzqbP3PMtsMZSvdsk' #Add your own Bot Chat ID bot_chatID = '1644262765' bot = telegram.Bot(token=bot_token) bot.sendMessage(chat_id=bot_chatID, text=message) bot.sendDocument(chat_id=bot_chatID, document=open(url, 'rb')) return jsonify("good")
def create_pdf_from_url(url, sha256): #:param url: url to retrieve # method to write pdf file app.logger.info('Creating PDF from URL:' + url) path = basePath + sha256 + '.pdf' app.logger.info('PDF Path:' + path) try: pdfkit.from_url(url, path) except Exception as e: # is needed on on windows, where os.rename can't override existing files. if os.path.isfile(path): return app.logger.error('Could not create PDF from the: ' + url) app.logger.error(traceback.format_exc(), e) return
def dfs(graph, visited, key, index): ''' DO depth first traversal and download each child webpage as a pdf. ''' # print(key, index) try: pdfkit.from_url(key, 'files_to_merge/' + str(index) + '_child.pdf') except: print("Couldn't print " + key) visited[key] = True for val in graph[key]: if val not in visited: index = index + 1 dfs(graph, visited, val, index) return None
def save(): if(request.form['myok']=='Save'): s = request.form['ta'] s=s.split('\n') pname=request.form['hfname'] print pname with open("temp.html", "w") as e: e.write("<html>"); for l in s: e.write("<pre>"+l+"</pre>"); e.write("</html>"); pdfkit.from_url('temp.html',os.path.join(app.config['UPLOAD_FOLDER'],pname)) return "File successfully saved"; else: return "No changes made"
def html_to_pdf(input, output, type='string'): """Convert HTML/webpage to PDF. For linux, install: sudo apt-get install wkhtmltopdf. Args: input (str): HTML Text, URL or file. output (str): Output file (.pdf). type (str): Types can be 'string', 'url' or 'file'. """ if type == 'url': pdfkit.from_url(input, output) elif type == 'file': pdfkit.from_file(input, output) else: pdfkit.from_string(input, output)
def create_pdf(self): __file__ = os.getcwd() file_html =os.path.join(__file__,"resources", "ficha.html") filename =os.path.join(__file__,"resources", "ficha.pdf") if os.path.exists(file_html): os.remove(file_html) f= open(file_html,"w+") html = self.create_html() f.write(html) f.close() config = pdfkit.configuration(wkhtmltopdf='/usr/bin/wkhtmltopdf') pdfkit.from_url(file_html, filename, configuration=config) return filename
def save_as_pdf(self, results, url): tmpdir = mkdtemp() try: options = {"load-error-handling": "ignore"} pdfkit.from_url(url, path.join(tmpdir, 'out.pdf'), options=options) with open(path.join(tmpdir, 'out.pdf'), 'rb') as pdf: pdf_import = AttachedFile.from_content(pdf, 'import.pdf', 'application/pdf') results.investigation.update(import_document=pdf_import) except Exception, e: print e
def save_as_pdf(self, results, url): tmpdir = mkdtemp() try: options = {"load-error-handling": "ignore"} pdfkit.from_url(url, path.join(tmpdir, 'out.pdf'), options=options) with open(path.join(tmpdir, 'out.pdf'), 'rb') as pdf: pdf_import = AttachedFile.from_content( pdf, 'import.pdf', 'application/pdf') results.investigation.update(import_document=pdf_import) except Exception, e: print e
def get_list_data(offset): res = requests.get(base_url, headers=headers, params=get_params(offset), cookies=cookies) data = json.loads(res.text) can_msg_continue = data['can_msg_continue'] next_offset = data['next_offset'] general_msg_list = data['general_msg_list'] list_data = json.loads(general_msg_list)['list'] for data in list_data: try: if data['app_msg_ext_info']['copyright_stat'] == 11: msg_info = data['app_msg_ext_info'] title = msg_info['title'] content_url = msg_info['content_url'] # 解决生成的pdf没有图片的问题,先请求内容然后将content_html构造成完整的html,使用pdfkit.from_string()生成pdf # try: # content_info = ws_api.get_article_content(content_url) # except: # return False # html = f''' # <!DOCTYPE html> # <html lang="en"> # <head> # <meta charset="UTF-8"> # <title>{title}</title> # </head> # <body> # <h2 style="text-align: center;font-weight: 400;">{title}</h2> # {content_info['content_html']} # </body> # </html> # ''' # pdfkit.from_string(html, './wechat_article/'+title+'.pdf') # 自己定义存储路径 pdfkit.from_url(content_url, './wechat_article/' + title + '.pdf') print('获取到原创文章:%s : %s' % (title, content_url)) except: print('不是图文') if can_msg_continue == 1: time.sleep(1) get_list_data(next_offset)
def run(self): options = {"disable-javascript": False} goog = pdfkit.from_url( "http://www.crtlabs.org/crt%20labs/smart%20devices/iot/2015/11/25/giftguide3.html", "./output.pdf", options=options, )
def email_sent(): # generate PDF pdf = pdfkit.from_url("127.0.0.1:5000/display_to_generate_pdf", "speed_estimation_result.pdf") # send email receiver_email = request.form["email"] EMAIL_ADDRESS = os.environ.get("EMAIL_ADDRESS") EMAIL_PASSWORD = os.environ.get("EMAIL_PASSWORD") msg = EmailMessage() msg["Subject"] = "Speed Estimation Result" msg["From"] = EMAIL_ADDRESS msg["To"] = receiver_email msg.set_content("Speed estimation result of your input video is created. Please check attached .pdf file to view the full report.") files = ["speed_estimation_result.pdf"] for file in files: with open(file, "rb") as f: file_data = f.read() file_name = f.name msg.add_attachment(file_data, maintype="application", subtype="octet-stream", filename=file_name) with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp: smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD) smtp.send_message(msg) return render_template("email_sent.html")
def create_pdf_from_url(url): if not os.path.isdir('pdfs'): os.mkdir('pdfs') if not os.path.exists('downloaded.json'): with open('downloaded.json', 'w') as f: f.writelines(json.dumps({create_pdf_from_url.request.id: url})) else: with open('downloaded.json', 'r+') as f: downloaded = json.load(f) downloaded[create_pdf_from_url.request.id] = url f.seek(0) f.truncate() f.writelines(json.dumps(downloaded)) pdfkit.from_url(url, 'pdfs/{}.pdf'.format(create_pdf_from_url.request.id))
def main(): url = 'https://automatetheboringstuff.com/' data = requests.get(url) soup = BeautifulSoup(data.text, "html.parser") container = soup.find_all('ul')[1] # [1] contains links to all chapters links = container.find_all('a') # links to chapters for link in links: name = link.string + '.pdf' # filename for pdf name = name.replace(' ', '_') # replace all spaces in name with _ name = '/home/shashank/Downloads/pdfkit/' + name #folder to save in path = url + link.get('href') # actual link to the web page pdfkit.from_url(path, name)
def render_pdf_from_url(bib_database): for entry in bib_database.entries: # Wenn es sich nicht um eine Internetquelle handelt if entry["ENTRYTYPE"] != "misc": continue # oder der Link zur pdf unter 'note' nicht bereits gesetzt wurde if "note" in entry: continue # oder es keine url gibt if "url" not in entry: continue print(entry) pdf_name = entry["ID"].replace(":", "_") + ".pdf" try: pdf = pdfkit.from_url(entry["url"], output_path=os.path.join(current_path, pdf_name)) note = "\href{run:" + rel_pdf_path + pdf_name + "}{export.pdf}" entry["note"] = note except Exception as e: print(e) entry["urldate"] = datetime.now().strftime(date_format) print(entry) print("\n") # Neue.bib Datei Speichern with open(os.path.join(current_path, "test.bib"), 'w') as bibtex_file: bibtexparser.dump(bib_database, bibtex_file)
def send_pdf(request): config = pdfkit.configuration(wkhtmltopdf="C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe") pdf = pdfkit.from_url("http://192.168.1.3:8000/form-pdf?slug={}".format(request.GET.get("slug")), False, configuration=config) response = HttpResponse(pdf,content_type='application/pdf') response['Content-Disposition'] = 'attachment; filename="example_file.pdf"' return response
def handle_request(config): # We are getting the url to generate from a form parameter options = {} options = request.values.getlist('options', type=float) print(options) # Converting post options group to dictionary listname = 'options' options = dict() for key, value in request.form.items(): if key[:len(listname)] == listname: options[key[len(listname)+1:-1]] = value if ('url' in request.form): print("URL provided: " + request.form['url']) pdf = pdfkit.from_url(str(request.form['url']), output_path=False, configuration=config, options=options) if ('html' in request.form): print("Html provided") pdf = pdfkit.from_string(str(request.form['html']), output_path=False, configuration=config, options=options) # If we are receiving the html contents from a uploaded file elif ('content' in request.files): print("File provided: " + str(request.files['content'])) f = request.files['content'] f.save(tmpfolder + secure_filename(f.filename)) pdf = pdfkit.from_file(tmpfolder + secure_filename(f.filename), output_path=False, configuration=config, options=options) return pdf
def download_pdf(): """ TODO: find solution for XHR inside of document. wkhtmltopdf does not seem to work with XHRs. """ return 'Not implemented yet =)' import pdfkit from flask import make_response options = { 'page-size': 'A4', 'orientation': 'Landscape', 'margin-top': '0.25in', 'margin-right': '0.25in', 'margin-bottom': '0.25in', 'margin-left': '0.25in', 'encoding': "UTF-8", 'no-outline': None } url = flask.url_for('index', _external=True) pdf = pdfkit.from_url(url, False, options=options) response = make_response(pdf) response.headers['Content-Type'] = 'application/pdf' filename = '%s_dashboard.pdf' % dt.now().strftime('%Y%m%d') response.headers['Content-Disposition'] = 'inline; filename=%s' % filename return response
def save_to_pdf(URL, pagenum): config = pdfkit.configuration( wkhtmltopdf='C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf.exe') options = { 'custom-header': [('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36' )] } try: pdfkit.from_url(URL, './' + str(pagenum) + '.pdf', configuration=config, options=options) except: return
def generate_pdf_view(request): data = { 'date': request.GET.get('date'), 'cid': request.user.userprofile.customer.pk, } url = request.build_absolute_uri(reverse('secret_pdf')) + "?" + urlencode(data) filename = slugify("{} {}".format(request.user.userprofile.customer.name, data['date'])) + ".pdf" options = { 'dpi': 300, 'viewport-size': '900', 'encoding': "UTF-8", 'no-outline': None } pdf = pdfkit.from_url(url, False, options=options) # set HTTP response headers response = HttpResponse(content_type="application/pdf") response["Cache-Control"] = "max-age=0" response["Accept-Ranges"] = "none" response["Content-Disposition"] = "attachment; filename={}".format(filename) # send the generated PDF response.write(pdf) return response
def download(self): dirlist = os.listdir(save_path)#目标路径所有文件名 filename, url = self.file_list.get() if (filename[8:] not in dirlist): #如果文件已存在则跳过 print(filename) print(url) try: pdfkit.from_url(url, filename, configuration=confg,) dirlist.append(filename[8:])#将刚下载的此文件名加入列表 #time.sleep(3) except: print('【出现错误,请检查文件是否已下载!】') else: print(filename,url,'【文件已存在!】')
def to_pdf(self, options={}, verbose=True): # Options are pdfkit.from_url options. See # https://pypi.python.org/pypi/pdfkit if verbose: print 'Generating report...' with open(self.__html_src_path, 'w') as html_out: html_out.write(self.__get_header()) html_out.write('\n'.join(self.__objects)) html_out.write(self.__get_footer()) if not verbose: options['quiet'] = '' pdfkit.from_url(self.__html_src_path, self.__report_path, options=options) report_path = self.get_report_path() if verbose: print 'Report written to {}'.format(report_path) return report_path