I want to programmatically fill a pdf using pdf-form gem
I was able to read the fields using the following code:
require 'pdf_forms'
pdftk = PdfForms.new('/usr/local/bin/pdftk')
fields = pdftk.get_fields('*/Desktop/sample_pdf.pdf')
for f in fields
print f.to_s
end
But when I tried to fill the Pdf with the provide synthax, I created only an empty, not openable pdf.
pdftk.fill_form '/path/to/form.pdf', 'target.pdf', ['Value Name'] => 'Value to be inserted'
Does anybody know a working solution?
Thanks in advance!!!
Additional Information:
the fields of the pdf are the following: Name, Address, Dropdown1, Dropdown2, Dropdown3, Check Box4, Check Box1, Check Box3, Check Box2, Text5, Button7, Text6, Group6, %
I tried it with filling just one field:
pdftk.fill_form 'sample_pdf.pdf', 'sample_pdf_filled.pdf', {["Name"]=>"Value to be inserted"}
The result: I have an pdf "sample_pdf_filled.pdf", which I cannot open, because it's empty. I thought my problem is the form-filling, but it seems, that the pdf is not created correctly.
Note: I am working on a mac (unix)
Has anybody a solution?
Method #fill_form is defined like this:
def fill_form(template, destination, data = {}, fill_options = {})
You example would produce something like this:
irb(main):001:0> { ['Value Name'] => 'Value to be inserted' }
=> {["Value Name"]=>"Value to be inserted"}
The data is supposed to be a Hash. If you want to specify a key with a space you can use it like this: :'Value Name'.
irb(main):002:0> { :'Value Name' => 'Value to be inserted' }
=> {:"Value Name"=>"Value to be inserted"}
I found a solution - for all MAC OS X 10.11 El Capitan User:
PDFtk Server on OS X 10.11
Related
I am utterly confused and lost with Nokogiri and web scraping in Rails. I need someone to explain to me how I can get article titles from a web site to list in a view in my Rails application. I can manage to retrieve the data in irb however I have no clue how I can get that same data to be displayed in a view I made.
I have watched a number of tutorials and read documentation and one thing that they do that confuses me the most is when they require nokogiri or open-uri in a their example ruby file what directory is that ruby file supposed to be placed in? Also is that file associated with any controller for it to be displayed in the particular view that I made?
I hope I am explaining my issue as clear as possible without any confusion as I am not trying to confuse myself anymore that i am in my explanation.
See, what I am trying to do is create an application where the user can register and sign in, after they are signed in they are redirected to a page with 3 links. Those links being Audi, BMW and Mercedes-Benz and depending on which link is clicked the user will be then directed to another page where they are returned back a list of articles that mention their desired choice.
I hope this explanation was helpful and I really hope someone can offer to help or give me some kind of documentation that will benefit me.
Thank you!
This is what I did in irb:
2.1.1 :001 > require 'rubygems'
=> false
2.1.1 :002 > require 'nokogiri'
=> true
2.1.1 :003 > require 'open-uri'
=> true
2.1.1 :004 > page = Nokogiri::HTML(open("http://www.dtm.com/de/News/Archiv/index.html"))
I then got this returned:
=> #<Nokogiri::HTML::Document:0x814e3b40 name="document" children=[#<Nokogiri::XML::DTD:0x814e37f8 name="HTML">, #<Nokogiri::XML::Element:0x814e358c name="html" children=[#<Nokogiri::XML::Text:0x814e3384 "\r\n">, #<Nokogiri::XML::Element:0x814e32d0 name="head" children=[#<Nokogiri::XML::Text:0x814e30f0 "\r\n">, #<Nokogiri::XML::Element:0x814e3028 name="title" children=[#<Nokogiri::XML::Text:0x814e2e48 "DTM | Newsarchiv">]>, #<Nokogiri::XML::Text:0x814e2c90 "\r\n">, #<Nokogiri::XML::Element:0x814e2bc8 name="meta" attributes=[#<Nokogiri::XML::Attr:0x814e2b64 name="charset" value="utf-8">]>, #<Nokogiri::XML::Text:0x814e2718 "\r\n">, #<Nokogiri::XML::Element:0x814e2664 name="meta" ...
(I got more but just put up a few lines of what was returned) I am assuming this is the raw data from the page.
I then put:
2.1.1 :008 > puts page
Which returned back the raw HTML content.
Finally I entered:
2.1.1 :014 > page.css("a")
Which returned back the all the links on the page.
I am hoping to help you with a real world example. Lets get some data from Reuters for example.
In your console try this:
# require your tools make sure you have gem install nokogiri
pry(main)> require 'nokogiri'
pry(main)> require 'open-uri'
# set the url
pry(main)> url = "http://www.reuters.com/finance/stocks/overview?symbol=0005.HK"
# load and assign to a variable
pry(main)> doc = Nokogiri::HTML(open(url))
# take a piece of the site that has an element style .sectionQuote you can use ids also
pry(main)> quote = doc.css(".sectionQuote")
Now if you have a look in quote you will see you will have Nokogiri elements. Lets have a look inside:
pry(main)> quote.size
=> 6
pry(main)> quote.first
=> #(Element:0x43ff468 {
name = "div",
attributes = [ #(Attr:0x43ff404 { name = "class", value = "sectionQuote nasdaqChange" })],
children = [
#(Text "\n\t\t\t"),
#(Element:0x43fef18 {
name = "div",
attributes = [ #(Attr:0x43feeb4 { name = "class", value = "sectionQuoteDetail" })],
children = [
#(Text "\n\t\t\t\t"),
#(Element:0x43fe9c8 { name = "span", attributes = [ #(Attr:0x43fe964 { name = "class", value = "nasdaqChangeHeader" })], children = [ #(Text "0005.HK on Hong Kong Stock")] }),
.....
}),
#(Text "\n\t\t")]
})
You can see that nokogiri has essentially encapsulated each DOM element, so that you can search and access it quickly.
if you want to just simply display this div element you can:
pry(main)> quote.first.to_html
=> "<div class=\"sectionQuote nasdaqChange\">\n\t\t\t<div class=\"sectionQuoteDetail\">\n\t\t\t\t<span class=\"nasdaqChangeHeader\">0005.HK on Hong Kong Stock</span>\n\t\t\t\t<br class=\"clear\"><br class=\"clear\">\n\t\t\t\t<span style=\"font-size: 23px;\">\n\t\t\t\t82.85</span><span>HKD</span><br>\n\t\t\t\t<span class=\"nasdaqChangeTime\">14 Aug 2014</span>\n\t\t\t</div>\n\t\t</div>"
and it is possible to use it directly in the view of a rails application.
if you want to be more specific and take individual components and traverse by looping the quote variable for elements one level down, in this instance you can:
pry(main)> quote.each{|p| puts p.inspect}
Or be very specific and get the value of an element ie the name of the stock in our example:
pry(main)> quote.at_css(".nasdaqChangeHeader").content
=> "0005.HK on Hong Kong Stock"
This is a very useful link: http://nokogiri.org/tutorials/searching_a_xml_html_document.html
Really hope this helps
PS: A tip for looking inside objects
(http://ruby-doc.org/core-2.1.1/Object.html#method-i-inspect)
puts quote.inspect
First, you can put nokogiri and openuri in the gemfile of your rails app, with that in place you don't need to require these libraries.
You flow to scrape the sites should be:
# put this code on your controller
web_site = params[:web_site] # could be http://www.bmw.com/com/en/
#doc = Nokogiri::HTML(open(web_site))
#then you can iterate over the document in your view
<% #doc.css('.standardTeaser').each do |teaser_bmw| %>
<p>teaser_bmw.css('.headline').text </p>
#other content of teaser you can search here
<% end %>
So, to scrape the web site you need to fetch the html from the web site and find what content you want to grab.
If you know some basics of css selector it will be very easy to do. Me example doesn't take in account if you want to save the data in a database... but if you want, you just need to create a table with the field you need to save and than create a record after parsing the html.
Is that made sense to you?
I'm trying to reset the Orientation EXIF field in my image using rmagick 2.13.1 and ruby 1.9.3p194, but it doesn't seem to save.
img = ::Magick::Image::read(local_source_path).first
img.get_exif_by_entry()
=> [["ColorSpace", "1"],
["ExifImageLength", "2448"],
["ExifImageWidth", "3264"],
["ExifOffset", "38"],
["Orientation", "6"]]
Orientation can also be acquired thru the properties
img.properties
outputs
{"date:create"=>"2014-01-08T17:26:10-08:00",
"date:modify"=>"2014-01-08T17:26:05-08:00",
"exif:ColorSpace"=>"1",
"exif:ExifImageLength"=>"2448",
"exif:ExifImageWidth"=>"3264",
"exif:ExifOffset"=>"38",
"exif:Orientation"=>"6",
"jpeg:colorspace"=>"2",
"jpeg:sampling-factor"=>"2x2,1x1,1x1"}
I try to set Orientation property to nil & save:
img['exif:Orientation'] = nil
Now,
img.properties
=> {"date:create"=>"2014-01-08T17:26:10-08:00",
"date:modify"=>"2014-01-08T17:26:05-08:00",
"exif:ColorSpace"=>"1",
"exif:ExifImageLength"=>"2448",
"exif:ExifImageWidth"=>"3264",
"exif:ExifOffset"=>"38",
"jpeg:colorspace"=>"2",
"jpeg:sampling-factor"=>"2x2,1x1,1x1"}
which looks correct.
img.write(local_dest_path)
but when I use get_exif_by_entry
img.get_exif_by_entry('Orientation')[0][1]
I still get 6 & not nil.
When I read the written file that was modified local_dest_path, I still get "6".
Is there a set_exif_by_entry function that I'm missing? Any ideas? Thanks
Couldn't set to nil, but I was able to set it to 1 during the write
img.write('local_dest_path') { self.orientation = ::Magick::TopLeftOrientation }
The constant UndefinedOrientation didn't work either. Other values available are listed in OrientationType.
My website allows users to add text to an image, and displays the result to them via an Ajax.request. Sometimes it works great, but other times the image is incomplete and javascript records the error "Image corrupt or truncated."
How can I make sure the file is completely written before sending the Ajax response back to the browser?
VIEW
new Ajax.Request('<%= url_for(:action => "update_image", :id => #user_image.id) %>?greeting=' + encodeURIComponent(elmgreeting.value), { onSuccess: document.getElementById("card_image").src=card_filename });
MODEL
def create_card
img = Magick::Image.read(self.input_image).first
# ... add the greeting to the image
img.write(self.card_filename)
self.card_width = img.columns
self.card_height = img.rows
self.card_size = img.filesize
end
CONTROLLER
def update_image
#user_image = UserImage.find(params[:id])
#user_image.greeting = params[:greeting]
#user_image.create_card
#user_image.save
render :layout => false
end
[I've also noticed the img.filesize value assigned to the card_size is often inaccurate, leading me to think it's being obtained too early, as well.)
I've tried everything I could find on stackoverflow and elsewhere, but just can't figure it out. Any help greatly appreciated.
FOUND MY PROBLEM: It was the Ajax request. It now works nicely:
new Ajax.Request('<%= url_for(:action => "update_image", :id => #user_image.id) %>?greeting=' + encodeURIComponent(elmgreeting.value), { onComplete:function(request){document.getElementById("card_image").src=card_filename}});
afaik the things you write should be already synchronous, there is probably something else that breaks things. what did your rails server log said? maybe there is an error happens and throws some errors and then your front-side tries to use it as an image source
I am currently building an XML export for a Real Estate app. using the Builder gem in Rails. And I am looking for a way to do the following:
<commercialRent>
"commercialRent figure"
<range>
...
</range>
</commercialRent>
I can't seem to find a way to implement the "Text Goes Here" part
My code:
b.commercialRent(period: "annual", plusOutgoings: self.plus_outgoings) {
self.rent_price;
b.range {
b.min(self.rent_psm_pa_min);
b.max(self.rent_psm_pa_max)
};
};
Returns:
<commercialRent period="annual" plusOutgoings="no">
<rentPerSquareMeter>
<range>
<min>1000</min>
<max>10000</max>
</range>
</rentPerSquareMeter>
</commercialRent>
Everything prints fine, except the self.rent_price is missing.
I can't figure it out.
You use the text! method to produce a text node:
- (Object) text!(text)
Append text to the output target. Escape any markup. May be used within the markup brackets as:
builder.p { |b| b.br; b.text! "HI" } #=> <p><br/>HI</p>
So something like this should do the trick:
b.commercialRent(period: "annual", plusOutgoings: self.plus_outgoings) do
b.text! self.rent_price
#...
end
I have an automated report tool (corp intranet) where the admins have a few text area boxes to enter some text for different parts of the email body.
What I'd like to do is parse the contents of the text area and wrap any hyperlinks found with link tags (so when the report goes out there are links instead of text urls).
Is ther a simple way to do something like this without figuring out a way of parsing the text to add link tags around a found (['http:','https:','ftp:] TO the first SPACE after)?
Thank You!
Ruby 1.87, Rails 2.3.5
Make a helper :
def make_urls(text)
urls = %r{(?:https?|ftp|mailto)://\S+}i
html_text = text.gsub urls, '\0'
html_text
end
on the view just call this function , you will get the expected output.
like :
irb(main):001:0> string = 'here is a link: http://google.com'
=> "here is a link: http://google.com"
irb(main):002:0> urls = %r{(?:https?|ftp|mailto)://\S+}i
=> /(?:https?|ftp|mailto):\/\/\S+/i
irb(main):003:0> html = string.gsub urls, '\0'
=> "here is a link: http://google.com"
There are many ways to accomplish your goal. One way would be to use Regex. If you have never heard of regex, this wikipedia entry should bring you up to speed.
For example:
content_string = "Blah ablal blabla lbal blah blaha http://www.google.com/ adsf dasd dadf dfasdf dadf sdfasdf dadf dfaksjdf kjdfasdf http://www.apple.com/ blah blah blah."
content_string.split(/\s+/).find_all { |u| u =~ /^https?:/ }
Which will return: ["http://www.google.com/", "http://www.apple.com/"]
Now, for the second half of the problem, you will use the array returned above to subsititue the text links for hyperlinks.
links = ["http://www.google.com/", "http://www.apple.com/"]
links.each do |l|
content_string.gsub!(l, "<a href='#{l}'>#{l}</a>")
end
content_string will now be updated to contain HTML hyperlinks for all http/https URLs.
As I mentioned earlier, there are numerous ways to tackle this problem - to find the URLs you could also do something like:
require 'uri'
URI.extract(content_string, ['http', 'https'])
I hope this helps you.