How to embed PDF in Rails - html

I'm pretty new to Rails development and I can't understand how to embed a PDF file in the browser. I already looked for others questions on the same subject like this and I'm dumbfounded. Already changed all the answer said but it just doesn't work.
This is my function
# create pdf function
def pdf
menu_pdf = File.join(Rails.root, 'app/assets/files/menulq2.pdf')
send_file(menu_pdf, filename: 'menulq2.pdf.pdf', disposition: 'inline', type: 'application/pdf')
end
And now I don't know what to do next.

The SO Answer you link to is about downloading/sending PDF files to/from your server. You needed to scroll down farther to read about displaying PDFs within your HTML views.
There's no reason to have a SomeModel#pdf method in your Model unless each record links to a specific PDF (e.g. if the model were Restaurant and you had a PDF menu for each restaurant.
You can just use an HTML embed tag:
<embed src="http://example.com/the.pdf" width="500" height="375" type="application/pdf">
As long as the path for 'app/assets/files' is in the asset pipeline
, you can use asset_path helper:
<embed src="<%= asset_path('menulq2.pdf') %>" width="500" height="375"
type="application/pdf">
The benefit here is if your assets are getting precompiled, the filename might be changed from menulq2.pdf to something like menulq2-60aa4fdc5cea14baf5400fba1abf4f2a46a5166bad4772b1effe341570f07de9.pdf, in which case, you as the developer, don't actually know what the PDF's filename actually is.
So the asset_path helper allows you to specify the simple filename and not worry about what asset compliation might have done to the name.
If that's too fiddly, or you know this asset isn't getting precompiled, you can hard-code it into the HTML:
<embed src="/assets/files/menulq2.pdf" width="500" height="375"
type="application/pdf">
To hard-code the URL you need to make sure you are serving static files by setting this to true in your environment files (/config/environments/*.rb):
For Rails 4: config.serve_static_files = true
For Rails 5+: config.public_file_server.enabled = true

Related

R Markdown knitr::include_url()

I am trying to include an R shiny app or a youtube video in an R Markdown report via
knitr::include_app() or knitr::include_url(). I have also tried with
<center>
<iframe src="URL" width="780px" height="610px" frameBorder="0"></iframe>
</center>
However, it never loads the page and only shows an empty frame. As soon as i switch to self_contained: false in the YAML, it works. However, I assume this must somehow work with a self contained HTML file no?
-> the ultimate goal would be to upload the report to rpubs
Thanks!

Exclude URI from Jekyll Escaping

In a webpage build with jekyll: How can I exclude a specific html-codeblock from escaping?
I want to embbed an iframe, but jekyll changes part of the url at built time.
I've tried to
disable markdown rendering using markdown="0" in the exact same block I wanted to stay the same.
put some content between the <iframe>-tags.
Both didn't change anything.
source code:
<iframe src="https://example.org/?q=run&do=true" width="100%" height="900px"></iframe>
output:
<iframe src="https://example.org/?q=run&do=true" width="100%" height="900px"></iframe>
Every time I use the & in an url, it becomes & in the static output. I want it to also be & after the jekyll build.
Edit:
This could also be a problem with kramdown parsing

Embed text files in html

I would like to display the content of a text file inside a HTML page (.rtf, .txt, .log,...) stored on the server.
I have tried with embed but seems that doesn't work.
<embed src="/path_to_text_file/text.rtf" width="500" height="300">
There is a "simple" method (or tag) to do that or I should scan for the content and print it with, for example, jQuery?
Something like this should do it:
<object data="/path_to_text_file/text.txt" type="text/plain"
width="500" style="height: 300px">
No Support?
</object>
Using a $.ajax() function with a .append() function inside you can easily grab the contents of a text document and display them on the page. Something along the lines of what you see below. Preform this on load of the page to immediately load the file in with the rest of the page.
$.ajax({
async:false,
url: 'folder/file.txt',
dataType: 'text',
success: function(data)
{
$('element').append(data);
}
});
Play around with it a little bit to get the correct result you are looking for. You could also use PHP but unless you really need to parse the data, PHP is a bit overkill in this situation.
is only for plugin content (flash, etc).
Try getting content using ajax, then write it with document.write;
Or use the include tag in a back end language (PHP, ASP, etc)
An iFrame might be useful in this context.
<iframe src="/path_to_text_file/text.rtf" width="500" height="300" frameBorder="0">
NOTE: apologies for the similarity to Keith V's answer and the fact he mentioned get requests - I only noticed his comment about get requests after posting my answer.
I find the following structure helpful, and allows me to style the text as a span rather than having to wield an object:
function append_url_content_to_div(url){
$.get(url, function(returned_data){
console.dir(returned_data);
var content = '<span>'+returned_data+'</span>';
$("#appendee_div").append(content);
});
}
append_url_content_to_div("https://dl.dropbox.com/s/euk874r7zd1cx0d/example_text.txt?dl=0"); //note that it has to be "dl." not "www." in dropbox
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="appendee_div"></div>
It works for me for dropbox content, but I don't see why it wouldn't work for publicly viewable text files. As you can see in the code, you need jquery.
I found the best way to insert HTML5 code into a website is to save the code onto a new text document and embed that document using . Then you can format the code using css and divs.
Just use php. Change the html format to php and use
echo file_get_contents("name_of_the_file.txt");
Simple as that.
You must use php because you have to output your text on the server side unless the info will not be shown to the client.
I think is the easiest way to do what you want.

Grails.View: Embedding pdf from resources

I have this problem embedding a .pdf file located in the folder web-app\files\[name] to the .gsp.
For example I have this file on this location:
\web-app\files\tester\test.pdf
I have this code in my .gsp having a model entity from its controller: name:String and fileName:String
<embed src="${resources(dir:'files/' + name, file:fileName + '.pdf')">
The problem is that I can't have it displayed on the embed folder and when I checked the code, it gives me src="/ProjectName/static/files/[name]/[fileName].pdf"
You need to ensure the resources plugin is configured to manage files in files/name directory
eg:
// What URL patterns should be processed by the resources plugin
grails.resources.adhoc.patterns = ['/images/*', '/css/*', '/js/*', '/plugins/*']
in Config.groovy
The <embed> tag also takes a mime type attribute which you should set.

How to convert en-media to img when convert enml to html

I'm working with evernote api on iOS, and want to translate enml to html. How to translate en-media to img? for example:
en-media:
<en-media type="image/jpeg" width="1200" hash="317ba2d234cd395150f2789cd574c722" height="1600" />
img:
<img src="imagePath"/>
I use core data to save information on iOS. So I can't give the local path of img file to "src = ". How to deal with this problem?
The simplest way is embedding image data using Data URI:
Find a Evernote Resource associated with this hash code.
Build the following Data URI (sorry for Java syntax, I'm not very familiar with Objective C):
String imgUrl = "data:" + resource.getMime() + ";base64," + java.util.prefs.Base64.byteArrayToBase64(resource.getData().getBody());
Create HTML img tag using imgUrl from (2).
Note: the following solution will allow you to display the image outside of the note's content.
On this page, you'll find the following url template:
https://host.evernote.com/shard/shardId/res/GUID
First compile the url from some variables, then point the html image src = the url.
In ruby, you might compile the url with a method similar to this one:
def resource_url
"https://#{EVERNOTE_HOST}/shard/#{self.note.notebook.user.evernote_shard_id}/res/#{self.evernote_id}"
end
...where self references the resource, EVERNOTE_HOST is equivalent to the host url (i.e. sandbox.evernote.com), evernote_shard_id is the user's shardId, and evernote_id is the user's guid.