print pdf file inside navigator weasyprint - django

I want to print pdf file inside my browser to let the choice to save. I am using weasyprint in django 2.0.2
i use this code in my views.py but firefox download it automaticaly and don't mind about the "inline". Any help?
response['Content-Disposition'] = 'inline; filename="gene_detail_description_print.pdf"'

Please take a look here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
Copying from there:
The first parameter in the HTTP context is either inline (default value,
indicating it can be display inside the Web page, or as the Web page) or
attachment (indicating it should be downloaded; most browsers presenting a
'Save as' dialog, prefilled with the value of the filename parameters if
present).
Content-Disposition: inline
Content-Disposition: attachment
Content-Disposition: attachment; filename="filename.jpg"
So you should use response['Content-Disposition'] = 'attachment; filename="gene_detail_description_print.pdf"' to allow the users to download the file and possibly rename it.

Related

postman render html response and execute JavaScript

I have an http api that give me html response, and I want to "preview" it.
But there is some javascript code in it, and without execute them, it won't give me the right page.
I currently manually copy & paste them in some aaa.html file and use chrome to open it(file://aaa.html), but I want to simplified those steps.
Is there anyway to do that in postman? or is there any postman alternative can do that?
There is an alternative to do this in Postman itself. You will have to use pm.visualizer. Just open your API request which is giving you the HTML response. Then go to the Test tab, add the lines below, and then click on the Visualize tab:
// save your html response in the template and then
const template = pm.response.text();
// set that template to pm.visualizer
pm.visualizer.set(template);
From Postman official documentation
Postman provides a programmable way to visually represent your request responses. Visualization code added to the Tests for a request will render in the Visualize tab for the response body, alongside the Pretty, Raw, and Preview options.
You need add to the Tests for a request:
var template = pm.response.text();
pm.visualizer.set(template);
and see result on the Visualize tab (after Pretty, Raw, and Preview options).
Postman result HTML with JS and CSS
To fix the error:
Refused to load the image 'file:///C:/some.png' because it violates the following Content Security Policy directive: "img-src http: https: data:".
if the content simply does not find (404 Not Found) it along a similar path 'file:///C:/some.file'
need to add the HTML tag to the section of the response body:
var response = pm.response.text();
var base = '<base href="https://some.domain/">";
var template = response.replace('<head>', '<head>' + base);
pm.visualizer.set(template);
this answer also solves the question in this comment.
For more information:
Postman Visualizing responses
HTML <base> tag in MDN Web Docs

Why do browsers omit XML tags?

(Better title, anyone?) Rendering some XML made with lxml.builder using a small Flask app in Python 3.6. The function makeXML in module mkX builds and returns the XML like so:
from lxml import etree as ET
...
def makeXML():
...
# myxml is type <class 'lxml.etree._Element'>
f = ET.tostring(myxml, method='xml', xml_declaration=True, encoding='utf-8', pretty_print=True)
return f
Where method=xml could be omitted, as it's the default. The Flask app does:
#app.route('/getXML')
def getXML():
xml = mkX.makeXML()
print(type(xml)) # xml is type <class 'bytes'>
return xml
When I go to [myurl]/getXML in Chrome or Firefox, I see this:
eggs bacon sausage spam
It omits the XML tags. Why does that happen? Hitting view source, I see this:
<?xml version='1.0' encoding='utf-8'?>
<someXML>
<reclist>
<dat>eggs</dat>
<dat>bacon</dat>
<dat>sausage</dat>
<dat>spam</dat>
</reclist>
</someXML>
With pretty_print=True it's nicely formatted. Without it:
<?xml version='1.0' encoding='utf-8'?>
<someXML><reclist><dat>eggs</dat><dat>bacon</dat><dat>sausage</dat><dat>spam</dat></reclist></someXML>
Looking at other webservices that return XML, the browser does not omit the XML tags, for example this one.
Does this mean that myxml isn't valid XML? If so, what's the difference & how should I fix it?
A browser renders HTML, not XML. Most browser try to show what's possible from a document. In your case they show you all text nodes but not the XML elements that have no meaning in HTML.
Check if the HTTP response includes a line saying
Content-Type: application/xml
Only if this is set can the browser decide to display the XML document.
As you can see when you open the source view, the XML is complete. Everything works as it is supposed to do.
For completeness' sake, in addition to Lutz Horn's answer, this is how to set Flask to return a specific mimetype:
...
from flask import Response
...
def getXML():
xml = mkX.makeXML()
return Response(xml, mimetype='application/xml')
Since the xml is records rather than text, 'application/xml' is preferable over 'text/xml', more info here.

how to view paperclip docx attachment in browser using rails

I have set the disposition property of the method to inline,so i am able to view pdf file in browser but same i want to be done for docx file.But instead of viewing in separate tab the file is downloading.
def download_emp
send_file #employee_document.document.path,
filename: #employee_document.document,
type: #employee_document.document_content_type,
disposition: 'inline'
end
But i want the docx attachment to be viewed in browser instead of downloading.
The link where you use the above method, try adding target: "_blank". Below is an example,
link_to "View", 'url', target: "_blank"
But downloading a doc file is the default feature of a browser, which cannot be replaced. But giving a try to the above code is worth.

How to prevent Django's response framework from stripping newlines of text files?

I'm trying to serve a text file in my view, and I can't seem to get Django to respect the newlines in the text file. The code is:
response = TemplateResponse(request, template='dashboard/email_template.txt', mimetype='text/plain', status=200)
response['Content-Disposition'] = 'attachment; filename=mail_template.txt'
return response
and the file looks something like:
key : {{ user.profile.secret_key }} # secret key
message: blah blah # Your message
When serving the file for download, the two lines are concatenated... When I open the template (email_template.txt) using whatever browser, text editor or IDE, the newlines are respected.
What makes Django (if this has something to do with Django?) concatenate these lines, and how can I prevent it?
Thanks in advance!
This is actually all Notepad's fault, but you can fix it by converting the template to use Windows-style newlines via unix2dos.

Django changing rendered url in a view

I want to change the display URL of a rendered response object. I have a single view "view1" and it is called by a URL, "localhost/foo/view1" . On some condition in view1 I want to change rendered URL to be displayed on browser to "localhost/foo/other/view1". I don't want to use HttpResponseRedirect. I only want to change the display URL in browser when the requested page is rendered.
No way you can do that. It would be phishing paradise if anyone could change the URL without redirecting.
Use redirects instead.
It's possible with the html5 history api, http://html5demos.com/history