from flask import Flask, request, jsonify, make_response, render_template, flash, redirect, url_for import re from flask_weasyprint import HTML, render_pdf import os
app = Flask(__name__)
URL_REGEX = re.compile( r'http(s)?://' # http or https r'(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+' )
def is_valid_url(url): if not URL_REGEX.match(url): return False if "blog.csdn.net" not in url: return False
return True
@app.route('/', methods=['GET', 'POST']) def CsdnToPdf(): if request.method == 'POST': url = request.form.get('url') # 当我不知道weasyprint会解析恶意 <link attachment=xxx>? url = url.replace("html", "") if is_valid_url(url): try: html = HTML(url=url) pdf = html.write_pdf() response = make_response(pdf) response.headers['Content-Type'] = 'application/pdf' response.headers['Content-Disposition'] = 'attachment; filename=output.pdf'