is there any way of printing a file on a printer in rails?
Let's assume I have a
def print_url
"/system/tickets/#{print_url_dir}/print.png"
end
and I want to call it with
resource.print_url
I tried already
<%= link_to 'Print', resource.print_url, :onclick => 'window.print();return false;'%>
But that just opened the printer dialog for the site I am on.
Sorry if it is a stupid question or I am missing something.
Thanks and all the best!
If you want to do this from a webpage - it's more of a Javascript task rather than Ruby/Rails one. You can do something like this:
<script type="text/javascript">
var WinPrint = window.open('', '_blank', 'left=0,top=0,toolbar=0,scrollbars=0,status=0');
WinPrint.document.write('<img src="<%= resource.print_url %>" />');
WinPrint.document.close();
WinPrint.focus();
WinPrint.print();
WinPrint.close();
</script>
It creates and open new window with content that contains only the image that would like to print, then print the page (which you already noticed that invokes the dialog) and the closes that new window. (I don't think there's a way to skip the dialog opening though).
You can check this answer for more information and examples.
Related
Using MVC 5 Razor Views.
I currently have a link to open a document that sits on the server in my about view as follows........
Basic Training <img src="~/Content/Images/Word.jpg" height="24" width="24" />
What I'd like is to be able to have a link to open this document at a particluar bookmark.
From what I have read so far, it would seem that the bookmark is specified after a # symbol. Unfortunately it doesn't seem to work and the document just opens from the start.
I've tried opening via an action using the #' notation as werll, but as yet, no joy.
FileStream fs = new FileStream(Server.MapPath(#"~\Content\My Doc - Basic Training.docx"), FileMode.OpenOrCreate, FileAccess.ReadWrite);
return File(fs, "My Doc - Basic Training.docx");
I've simply been appending #BoomarkName to the filename. No joy as of yet.
Is it possible?
If so could someone please point me in the right direction.
Just seems to work with #bookmark_name, as explained in How to create a hyperlink from an HTML page to a bookmark in Word.
So:
Basic Training <img src="~/Content/Images/Word.jpg" height="24" width="24" />
I have a link like so
= link_to "https://twitter.com/share", class: "twitter-share-button", data: { url: "https://google.com", text: hack.body, via: "GhettoLifeHack_", hashtags: "ghettolifehack" } do
= image_tag "Tweet", alt: "Social Twitter tweet button"
and no matter how much I change the data-url value, the pre-tweet confirmation page always prepopulates the tweet form field with the url of the referring page, not the one I specified. It also ignores my custom data-text as well.
Why is this happening?
I also have this minified script
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
that I got from here https://about.twitter.com/resources/buttons#tweet
Removing that script doesn't seem to change anything.
edit: upon trying using :'data-url' attributes directly, the output html is the same.
I am testing hardcoded strings and dynamically generated urls at the same time. The first is the dynamic one.
<a class="twitter-share-button" href="https://twitter.com/share" data-via="GhettoLifeHack_" data-url="http://localhost:3000/hacks/1" data-text="asdf comment body" data-hashtags="ghettolifehack">
<img src="/images/Tweet" alt="Tweet" title=""></img>
</a>
The second is the hard coded strings
<a class="twitter-share-button" href="https://twitter.com/share" data-via="GhettoLifeHack_" data-url="httpL//google.com" data-text="custom text" data-hashtags="ghettolifehack">
<img src="/images/Tweet" alt="Tweet" title=""></img>
</a>
I've tested on development and in production. Both have the same behavior of pre-populating the tweet form with the referring url, rather than the specified url and text.
This works in Chrome for me but not in Firefox 32
The code provided by you is perfectly fine and should work as expected.
Many site issues can be caused by corrupt cookies or cache. Try to clear both cookies and the cache. I would suggest you to look into the following link to see why it is not working in firefox
The issue was specific to firefox browser. I'm not sure what addon or setting is causing the conflicts, but it is working perfectly find in chrome, including the popup window.
I am using TCPDF library to generate some reports, and i want to send the PDF file to print option of browser as simple we press CTRL+P, I need this because it is slip.
I used all parameter for Output but it is downloading the file directly.
$pdf->Output('slip.pdf', 'I');
I also placed the F,D,S,E,FI and FD instate of I but it doesn't work. And I also used header
header('Content-Type: application/pdf');
$pdf->Output('example_001.pdf', 'FD');
but again it doesn't work. Any solution? Please!
Add
$pdf->IncludeJS("print();");
just before $pdf->Output...
You need something like the example below. You would need to intercept print request (print automatically on page load, print button click, etc.) and then call printTrigger function.
<html>
<head>
<title>Print PDF</title>
<script>
function printTrigger(elementId) {
var getMyFrame = document.getElementById(elementId);
getMyFrame.focus();
getMyFrame.contentWindow.print();
}
</script>
</head>
<body>
<iframe id="iFramePdf" src="http://pdfurl.com/sample.pdf"></iframe>
...
</body>
</html>
What you are trying to do is not within the specification of the TCPDF API.
http://www.tcpdf.org/doc/code/classTCPDF.html#a3d6dcb62298ec9d42e9125ee2f5b23a1
I believe you would need to use JavaScript to implement this feature the way you are proposing.
Add $pdf->IncludeJS("print();"); just before $pdf->Output...
It's working for me.
ffmepg and qtfaststart to process my video uploads. The converting is working fine and the video gets uploaded. My problem is that i can't display it. When I first tried <%= video_tag(#post.file.url) %> I just get a blank area. I've also tried jplayer, got the installation and all working and when i tried to call my video path it doesn't work.
Here is the js code when I tried with jplayer.
$(document).ready(function(){
$("#jquery_jplayer_1").jPlayer({
ready: function () {
$(this).jPlayer("setMedia", {
m4v: "<%= video_tag(#post.file.url) %>"
});
},
swfPath: "/",
supplied: "m4v",
solution: "flash, html"
});
});
</script>
I don't know how to call the uploaded video and display it, Please really need some help!
please always read the documentation. otherwise you will get comments a la RTFM.
when you use video_tag it inserts an html5 video element into the page. read about it here: http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html#method-i-video_tag
<video src="/trailers/hd.avi" width="16" height="16" />
this is probably not what you want for your jPlayer. when you inspect the html source, this should be obvious. i did never use jPlayer, but i assume that it would like to be pointed to the streaming source, which i guess is #post.file.url in your case.
Folks,
I am having an interesting problem. I have some javascript on the webpage which opens a popup window when clicked. I am trying to find the title of the window so that I can click on that, the window has the following two buttons "Cancel" and "Save File". Here is what I am doing in my ruby code:
#windows = #browser.windows #this should return an array, so #windows is an array
p #windows[1] #output of this is #<Watir::Window:0x115c796cc located=true>
puts "This is the title of the second window---->"+#windows[1].title #this puts blank
The problem that I am seeing is why does my windows object does not have any variables when I print it out using p #windows[1] also why is the title not printed when I do #windows[1].title. My goal is click on the "Download the file" button of the popup window
This is the piece of HTML that I have:
<td>
<a onclick="window.open(this.href);return false;" href="/search/searches/1563/exports/1017">6175-1017-20120418181521-karnire.eml.zip</a>
</td>
The other thing that I tried is doing something like this in my code:
#windows = #browser.windows
#browser.window(:title => #windows[1].title).use do
#browser.button(:value => "Save File").click
end
for the above I get an error like this:
Unable to locate window "{639686d9-4641-aa41-bf6f-3ba89659d921}" (Selenium::WebDriver::Error::NoSuchWindowError)
I'd start with the information provided here on the watir-webdriver blog
if that does not work, then try looking at the watir-wiki page on file downloads?
It's a little dated (not having been updated in a year, also using autoit not rautomation) but it might be enough to get you going.
It might be that Watir isn't waiting for the window to load. Try putting a sleep(10) after the click.