Bootstrap CSS is not applied in weasyprint - python

I have made a flask application which generates .pdf from html with weasyprint and send it as attachment. But apparently bootstrap 4 css is not applied. I can't find a solution.
It is working well with pdfkit but I need weasyprint, since pythonanywhere.com does not support pdfkit.
I have tried linking bootstrap, using bootstrap css as file in my html, but there was no difference.
This is my python part, which generates and sends pdf.
#app.route('/pdf_send', methods=('GET', 'POST'))
#login_required
def pdf_send():
rendered = render_template(
'pdf_send2.html',
name=name_g,
surname=surname_g,
email=email_g,
address=address_g,
invoice_no=invoice_no_g,
dict_g=dict_g,
bendra_suma_g = ('%.2f' % round(float(bendra_suma_g[0]), 2)),
send=send_g,
today=today_g
)
css_file = ('static/bootstrap.css')
filename = 'SF-' + invoice_no_g +'.pdf'
html = HTML(string=rendered)
css = CSS(filename=css_file)
html.write_pdf(filename, stylesheets=[css])
send_email(filename, email_g)
return redirect(url_for('index'))

I know this is an old thread but I stumbled over it while looking for an answer to the same question, so I'm adding my workaround in case it helps anyone else. I'm going to start by pointing out that although this does work Bootstrap really isn't intended to be used for printing, and weasyprint ignores anything inside a #media query so you're going to need to add a lot of css anyway in order to make it render correctly so it's probably not worth it.
Using the code from the original question, if you add the bootstrap cdn css to the stylesheets list it will render:
html = HTML(string=rendered)
css = CSS(filename=css_file)
html.write_pdf(filename, stylesheets=[css, "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"])
However, given the issues with Bootstrap rendering, I found this trick to be considerably more useful for applying fonts.

weasyprint doesnt work well with bootstrap .
This is the reference Link to Github site for clarity.
Whatever bootstrap classes you write will be ignored by WeasyPrint.
You can see the "Ignored ..." messsages in WeasyPrint error log(if you ever initilised).
So its better to use CSS for the print-pages

Related

With bottle.py, how do I properly render static file requests of templates?

I am experimenting with bottle.py, and I'm coming across some problems with requesting static templates vs rendering them.
I have some basic routes:
#route("/feed")
def show_feed():
# query database, calculate things, etc.
# code to show feed (which is dynamic)
#route("/submit")
def show_submit():
# query database, calculate things, etc.
# code to show submit
#route("/<filename:path>")
def serve_static(filename):
# code to simply return static files
I also have some templates:
views/submit.html
views/feed.html
There are no problems with the web server itself. It works as it is told. The problem is when I use links in my templates, as usual, to go from page to page:
Go back to the feed
When a user clicks on that link or manually enters ".../feed.html" or ".../submit.html", the URL .../feed.html is requested instead of /feed, and Bottle routes that to the serve_static(filename) function. As a result, the template is not rendered - instead, the static template is returned, complete with ugly things like "{{article[0]}}" and "% end % end".
How can I get Bottle to render these templates properly?
Is there a way for Bottle to know when to render templates when they are requested as static files? Is it considered an okay practice to change href attributes to what the server should expect? Is there something I'm not considering?
The problem is very simply that
<a href="feed.html">
has a wrong href given your URL pattern. Just change it to
<a href="feed">
It's also a good idea, as BrenBarn suggested, to move the templates into their own separate subdirectories, away from the one from which you want to serve truly static public files. However, per se, that wouldn't solve your problem -- you'd just get a 404 on clicks on the link in question. The core issue is fixing that <a>'s href!

Weasprint and Twitter Bootstrap

Does anybody have any experience rendering web pages in weasyprint that are styled using twitter Bootstrap? Whenever I try, the html renders completely unstyled as if there was no css applied to it.
I figured out what the problem was. When I declared the style sheet i set media="screen", I removed that tag element and it seemed to fix it. Further research indicated I could also declare a separate stylesheet and set media="print".

Python : Rendering part of webpage with proper styling from server

I am building a screen clipping app.
So far:
I can get the html mark up of the part of the web page the user has selected including images and videos.
I then send them to a server to process the html with BeautifulSoup to sanitize the html and convert all relative paths if any to absolute paths
Now I need to render the part of the page. But I have no way to render the styling. Is there any library to help me in this matter or any other way in python ?
One way would be to fetch the whole webpage with urllib2 and remove the parts of the body I don't need and then render it.
But there must be a more pythonic way :)
Note: I don't want a screenshot. I am trying to render proper html with styling.
Thanks :)
Download the complete webpage, extract the style elements and the stylesheet link elements and download the files referenced the latter. That should give you the CSS used on the page.

How can I use css files in django?

I'm using a template with block tags to include a css file.
Unsure on how to manage css files, I put this in my urls.py:
url(r'style.css$','portal.views.css')
with this in my views
def css(request):
return render_to_response('style.css')
When I use chrome's "inspect element" on any page that uses the template I can see all the css in the file, however nothing at works on the page itself. To test it I added
body
{
display:none;
}
But still no changes.
Do I need to render the response in a different way?
You shouldn't serve static files (like css files) that way.
Django provides a specific solution for that, make sure you read the complete and comprehensive documentation on that.

Pylons: Routes information availability from templates

I'm building a Pylons application using evoque as our templating engine, though I think my question is relevant to other template engines. I have a base template that I'm using for our pages, and that base template does all the includes for CSS and Javascript files. I'd like to perform conditional test to include/exclude CSS and Javascript files based on the actual page being display. Is there a way to access the routes information from the template, in other words to get the /{controller}/{action} information? This would allow me to get only the relevant CSS and Javascript files for that page based on the controller/action combination.
Thanks in advance,
Doug
You can pull the controller and action information from environ['pylons.routes_dict']['controller'] and ['action'].
I'm not sure if environ is passed into the tmpl_context by default, but if not, you can just add something like this to the BaseController.__before__ method:
c.routes_dict = environ['pylons.routes_dict']
Then reference c.routes_dict['controller'] in your template.

Categories

Resources