Inserting hyperlinks into pdf generated with pisa - python

Currently I am generating a pdf from a html template in django/python.
Here is a relevant snipit from my view
result = StringIO.StringIO()
html = render_to_string(template='some_ref/pdf.html', { dictionary passed to template},)
pdf = pisa.pisaDocument(StringIO.StringIO(html), dest=result)
return HttpResponse(result.getvalue(), content_type='application/pdf')
And my template is an html file that I would like to insert a hyperlink into. Something like
<td style="padding-left: 5px;">
{{ some_other_variable }}
</td>
Actually, the pdf generates fine and the template variables are passed correctly and show in the pdf. What is inside the a tag is highlighted in blue and underlined as if you could click on it, but when I try to click on it, the link is not followed. I have seen pdfs before with clickable links, so I believe it can be done.
Is there a way I can do this to make clickable hyperlinks on my pdf using pisa?

it works with the complete url: http protocol and domain
{{ some_other_variable }}

Related

Embedded HTML code in Python string doesn't appear properly in Django

I'm facing a problem when I embed HTML code in my python's view.py
Basically, my objective is to customize the color of certain words only (based on the input). I want to do it by modifying the view.py
For example (my view.py):
def home(request):
form = request.POST.get('uncorrected')
texts = str(form) + '<span style="color: red">test text but this section is red</span>'
return render(request, 'corrector/home.html', {'text': texts})
Inside my index.html:
<textarea type="text" id="textarea" name="uncorrected">
{{ text }}
</textarea>
However, when I type "my text" in the textarea it displays only:
my text <span style="color: red">test text but this section is red</span>
It doesn't make the text red, it directly displays the code.
How can I make it work?
Django automatically escapes HTML to prevent XSS attacks. In order to render HTML as HTML, you just pipe in safe.
{{ text|safe }}
Django will still escape the HTML, but it will render it as well

Create hyperlink and give it an appealing name in Python

I am trying to rename my hyperlink to place in a pdf file. Thus, I do not want to give to the user a massive long link.
Let's say my link is like:
https://www.google.com/search?q=images+of+dogs&rlz=1C1OKWM_esES969ES969&sxsrf=AOaemvJFDb3FKdXO1Yqb3A1BdjWNfw0Edg:1632237403618&tbm=isch&source=iu&ictx=1&fir=D5X9VdSPli-xYM%252CHUMB4Zy1hHwFaM%252C_&vet=1&usg=AI4_-kShuarwW69ikZrP2YUHRVOpRHKKfQ&sa=X&ved=2ahUKEwiPs4aVrpDzAhUR1RoKHQiNAZIQ9QF6BAgPEAE&biw=2133&bih=1013&dpr=0.9#imgrc=D5X9VdSPli-xYM
And I want it to appear in the pdf like:
"Link to picture"
My code:
texto_body=f"Hi,<br> <br> This is a test with a link {link} <br> <br> Thanks,"
body=f"""\
<html>
<body>
<p style="color:black;"> {texto_body}</p>
<img src="cid:image1" alt="Logo" style="width:90px;height:90px;"><br>
</body>
</html>
"""
Solved. I found that text of the link
is the way to set up hyperlinks with a given name

Cannot get specific image from NYTimes website

I am scraping metadata from the New York Times' website. I'm looking to gather three pieces of information:
Headline
Article URL
Thumbnail image
I have been successful in gathering all three except in cases where the NYTimes homepage shows the article's image on the homepage. In that case, I've tried to capture that homepage thumbnail image, but have been unsuccessful. Here is my code so far:
for item in soup.select('.story-wrapper'):
try:
headline = item.find('h3').get_text()
link = item.find('a')['href']
image = item.select('.css-hdqqnp')
The css selector .css-hdqqnp references the class of the thumbnail image for article images that are displayed on the NYTimes homepage (as opposed to being just text).
How can I get the thumbnail image for an article if it's already displayed on the homepage, as opposed to being available only on the article page (which I've already successfully gathered)?
The problem is that the HTML structure is
<div class="..." span="4">
<div class="....">
<section class="story-wrapper"> ... </section>
</div>
</div>
<div class="..." span="6">
<div class="....">
<!-- ... your nested img-tag inside a div-tag with css class 'css-hdqqnp' -->
</div>
</div>
That is, the image is not inside the section-tag. Instead, it's inside the next sibling tag of the section's grandparent tag. Consequently, you could search for the image thumbnails like this:
for item in soup.select('.story-wrapper'):
headline = item.find('h3').get_text()
link = item.find('a')['href']
if (sibling := item.parent.parent.next_sibling) is not None:
if (image := sibling.find("img")) is not None:
image_url = image["src"]

Using Markdown2 with Django

I have never asked a question here before, please bear with me. I am working on a wiki project that has a requirement to convert markdown files using markdown2.
return render(request, "encyclopedia/entry.html", {
"content": markdown2.markdown(util.get_entry(title)), "title": title
})
Above is how I pass it to the HTML page and it renders on the page with the proper HTML tags, but it doesn't seem to use them. Below is how it appears on the browser.
<pre><code> # HTML
</code></pre>
<p>HTML is a markup language that can be used to define the structure of a web page. HTML elements include</p>
<ul>
<li>headings</li>
<li>paragraphs</li>
<li>lists</li>
<li>links</li>
<li>and more!
most recent major version of HTML is HTML5.</li>
</ul>
I am passing it directly to a Django template with the safe filter included as shown below.
<textarea name="content" rows="5" cols="50" readonly>
{{ content|safe }}
</textarea><br>
Thank you ahead of time, I hope I provided enough information to make my problem clear.
it looks like your content is going inside a <textarea> form field...that's going to prevent the browser from interpreting the HTML and just show exactly what is passed over.
Change to a <div> or something and it should work.

Python flask generator function not working with JQuery-mobile

I've been stuck with the following problem for a while, and i can't seem to get it to work on this Python flask web application i've been developing:
1) my flask view -> obtains image data from a local database and passes the data via a generator to a template
My flask route/view code:
#app.route(#app.route('/viewer',methods=['GET'])
def viewer():
archive = request.args.get('filepath')
arc = rarTools()
pagetotal = arc.pageTotal(archive)
def g():
for n in range(pagetotal):
data = arc.getPageb64(archive,n+1)
print(n+1)
yield data
return Response(stream_template('viewer.html',data=g()))
2) My HTML template with JQuery Mobile UI obtains this data and simply displays all the image data on 1 page
My HTML viewer.html with JQuery mobile UI:
<body style="background-color: black">
<div data-role="page" class="page" id="mainpage" data-theme="b">
<div data-role="main" class="ui-content">
<div id="frame" align="middle" style="width: 100%">
{% for page in data %}
<img id="page_img" src="data:image/png;base64,{{ page }}" width="80px" height="auto"/>
{% endfor %}
</div>
</div>
</div>
</body>
My HTML viewer.html WITHOUT JQuery mobile UI:
<body style="background-color: black">
<div id="frame" align="middle" style="width: 100%">
{% for page in data %}
<img id="page_img" src="data:image/png;base64,{{ page }}" width="80px" height="auto"/>
{% endfor %}
</div>
</body>
My problem is how long it takes to load pages when there are many images in my database. The load time is vastly different when I use JQuery Mobile and when I don't:
Because i'm using a generator to pass the data to the template, the images are loaded dynamically with the page loading immediately and each page appearing as the image data is generated on the flask back-end when I don't use JQuery Mobile to style the page.
When I use JQuery Mobile on the other hand, the page only loads once all the image data is passed even though i'm using a generator.
How can I get my JQuery Mobile template page to refresh after every yield, so that
The page loads immediately irrespective of image count?
The pages refreshes after each new image data "yield" so the the images are loaded dynamically? - like it works when I don't use JQuery Mobile?
I am aware that it has something to do with the - either: 'pageinit' or 'pageload' events with Jquery Mobile, but for the life of me, i can't seem to get the above code to work!!
Any assistance would be much appreciated, thanks in advance!

Categories

Resources