how to view paperclip docx attachment in browser using rails - ruby-on-rails-4

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.

Related

How to get the text file which is uploaded from android device and store it in a folder using django?

I want to save the text file or zip file in folder which can be uploaded in mobile devices(Android,IOS) using django framework.How to get the file in request ? Thanks in advance
If i got your question, this should be the answer. In your models.py file add a column for storing this upload:
yourfield = models.FileField(upload_to=path_of_the_folder_where_you_want_to_save)
And make the form on this model.

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

print pdf file inside navigator weasyprint

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.

Rails combining pdfs from action stream

I have an action in my controller #applications that renders a html page of my websites application page. When I specify the path for this action with the :pdf format it will render the page as a pdf (which works correctly).
I am writing another action #applications_print_version that needs to contain the rendered view from #applications plus some other pdfs (from a list of urls). Using combine_pdf I have been able to get the list of pdfs via urls working, but I can not get the #applications view to be added to the combined pdf in the #applications_print_version working.
Here is what I have so far.
def applications_print_version
html = render_to_string(action: :applications)
pdf = WickedPdf.new.pdf_from_string(html)
new_pdf = CombinePDF.new
new_pdf << CombinePDF.parse(pdf)
#List of pdfs I got from somewhere else
#pdf_attachments.each { |att| new_pdf << CombinePDF.parse( Net::HTTP.get( URI.parse( att.url ) ) ) }
send_data new_pdf.to_pdf, :disposition => 'inline', :type => "application/pdf"
end
This solution does have all the data, but the pdf variable has no styling. I can't seem to get this to work.
Thanks to a local community support I have managed to get it to work. There were a few things I needed to fix.
The layout that I was using to render #applications was using my standard pdf layout that I used for PDFKit. I duplicated the layout and replaced
<%= stylesheet_link_tag 'application' %>
with
<%= wicked_pdf_stylesheet_link_tag 'application' %>
One I did that I could render the #applications action with the layout the WickedPDF needed.
html = render_to_string(action: :applications, layout: 'wicked_pdf')
I ran into another issue. There is a known issue with WickedPDF https://github.com/mileszs/wicked_pdf/issues/470
So I had to remove any instances of #import "bootstrap" in my stylesheets which is not ideal, but I could not resolve the above issue.
Any now the #applications_print_version works correctly!
If anyone can do better, please let me know, I would like to know :)

pdfkit not rendering images from cdn + rails 4.2 + pdfkit

I am trying to convert a html page that displays images from facebook cdn to pdf using pdfkit. I am using rails 4.2, pdfkit 0.6.2 and wkhtmltopdf-binary 0.9.9.3.
# Gemfile
gem 'pdfkit'
gem 'wkhtmltopdf-binary'
# controller
def generate_pdf
#booklet = Booklet.find params[:id]
#cover = Image.last
#images = #booklet.images.sort_by(&:uploaded_at)
respond_to do |format|
format.html
format.pdf do
html = render_to_string(layout: true , action: "generate_pdf.html.haml")
kit = PDFKit.new(html, page_size: 'A4', print_media_type: true)
kit.stylesheets << "#{Rails.root}/app/assets/stylesheets/application.scss"
pdf = kit.to_pdf
send_data pdf, filename: 'booklet.pdf', type: 'application/pdf', disposition: 'inline'
end
end
end
# application.scss
#import 'bootstrap';
#import 'custom';
#import 'jquery.booklet';
#import 'bootstrap-datepicker3';
# haml
= link_to 'Download Booklet', generate_pdf_booklet_path(#booklet, format: 'pdf'), class: 'btn btn-primary'
# config/application.rb
require 'pdfkit'
config.middleware.use PDFKit::Middleware
# config/initializers/mime_types.rb
Mime::Type.register "application/pdf", :pdf unless Mime::Type.lookup_by_extension(:pdf)
example facebook cdn image urls are
https://scontent.xx.fbcdn.net/hphotos-prn2/v/t1.0-9/s720x720/560041_10200752471482799_613254552_n.jpg?oh=900fe52ecc9b93e044cae4917f538626&oe=559F41E9 and https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-xap1/t31.0-8/s720x720/906468_10201023370815113_668460846_o.jpg
When i send a pdf request, I get following output on the server log
Rendered booklets/generate_pdf.html.haml within layouts/application (671.3ms)
QSslSocket: cannot resolve SSLv2_client_method
QSslSocket: cannot resolve SSLv2_server_method
QAbstractSocket::connectToHost() called when already looking up or connecting/connected to "fbcdn-sphotos-e-a.akamaihd.net"
QAbstractSocket::connectToHost() called when already looking up or connecting/connected to "fbcdn-sphotos-e-a.akamaihd.net"
QSslSocket::connectToHostEncrypted() called when already connecting/connected
QSslSocket::connectToHostEncrypted() called when already connecting/connected
QAbstractSocket::connectToHost() called when already looking up or connecting/connected to "fbcdn-sphotos-b-a.akamaihd.net"
QAbstractSocket::connectToHost() called when already looking up or connecting/connected to "fbcdn-sphotos-b-a.akamaihd.net"
QSslSocket::connectToHostEncrypted() called when already connecting/connected
QAbstractSocket::connectToHost() called when already looking up or connecting/connected to "fbcdn-sphotos-f-a.akamaihd.net"
QAbstractSocket::connectToHost() called when already looking up or connecting/connected to "fbcdn-sphotos-c-a.akamaihd.net"
Rendered text template (0.0ms)
Sent data booklet.pdf (1.4ms)
The pdf is getting generated but the local images(app/assets/images) and the cdn images arent rendered in the pdf. Also, the stylesheet isnt applied in the pdf. What am I missing?
Have created a sample repository for the above problem. Here: https://github.com/prasadsurase/topdf
I finally managed to fix the images issue. I removed the wkhtmltopdf-binary gem from the Gemfile and installed the wkhtmltopdf library(version 0.9.6) on the box as
sudo apt-get install wkhtmltopdf
Unfortunately, I am not able to install the same version on the server. A better approach would be to download the binary and keep it in your application in the bin folder and specify the binary's relative location in the pdfkit initializer. This would remove the version issues. FYI, the stylesheet issue still persists( PDFkit *.css stylesheets not being applied)
This issue usually caused by the SSL library of QT. If updating wkthmltopdf doesn't help, finding a suitable libssl-dev version may help to solve the issue.