Error invoking PDFTK when modify PDF in Rails 4 - ruby-on-rails

I am developing Rails 4 application where have to modify existing PDF file.
User can write some comments and click then comments write in existing PDF as well. For this, i used gem 'pdf-toolkit'
But i got below error:
Error invoking PDFTK
My Code:
my_pdf = PDF::Toolkit.open("Credit_One.pdf")
my_pdf.updated_at = Time.now # ModDate
my_pdf["SomeAttribute"] = "Some value"
my_pdf.save!
Where is wrong any one have a idea.
Thanks

Related

Invalid argument # rb_sysopen in Ruby on Rails application

I know this is the common question in this forum, but I tried last nearly 4 hours one by one not working for me. Worked before another method & another resource but not in this method.
Error below:
Invalid argument # rb_sysopen - https://example.s3.amazonaws.com/uploads/Data.pdf
When I use like below: Static File
#file = File.open("#{Rails.root}/public/Data.pdf")
Then
File.new(#file)
That is working but when like below:
Dynamic File
#file = File.open("#{papers.paper.url}") #=> output url: https://example.s3.amazonaws.com/uploads/Data.pdf
Then
File.new(#file)
That is showing error. I have tried string like w w+ r r+ etc.
What I'm doing wrong with this code?
Thanks

Attach a pdf in asset pipeline using ActionMailer Rails 4

I'm trying to attach a file to an email. The file is in assets/downloads/product.pdf
In the mailer, I have:
attachments["product.pdf"] = File.read(ActionController::Base.helpers.asset_path("product.pdf"))
I've tried:
attachments["product.pdf"] = File.read(ActionController::Base.helpers.asset_url("product.pdf"))
...and even:
attachments["product.pdf"] = File.read(ActionController::Base.helpers.compute_asset_host("product.pdf") + ActionController::Base.helpers.compute_asset_path("product.pdf"))
I always get the same error:
EmailJob crashed!
Errno::ENOENT: No such file or directory - //localhost:3000/assets/product.pdf
...or a variation on the theme. But even when I try using asset_url in the view or just put the url in the browser it works:
http://localhost:3000/assets/product.pdf
I've also tried using straight up:
File.read("app/assets/downloads/product.pdf")
File.read("downloads/product.pdf")
...which works in dev environment but not on staging server (heroku). Error is still:
Errno::ENOENT: No such file or directory - downloads/product-market-fit-storyboard.pdf
Also tried:
File.read("/downloads/product.pdf")
File.read("http://lvh.me:3000/assets/product.pdf")
...don't work at all.
Ideas?
you should use syntex like this.it is work for me may be will work for you also.
File.open(Dir.glob("#{Rails.root}/app/assets/downloads/product.pdf"), "r")
When using mailer, you shouldn't use assets pipline. Asset pipeline would be useful if you wanted to have link to a file inside your email. When rendering an email, action mailer has access to files in app directory.
Please read about attachments in action mailer guide. As you can see, you just need to pass path to a file, not url:
attachments['filename.jpg'] = File.read('/path/to/filename.jpg')

Adding one more row to existing excel file in ruby

I am trying to simply add one more row to an existing excel file using spreadsheet gem.
test_url = "#{Rails.root}/data/Skipped_Records.xls"
if File.exists?(test_url)
book = Spreadsheet.open(test_url)
else
book = Spreadsheet::Workbook.new
book.create_worksheet
end
sheet = book.worksheet(0)
last_index = sheet.row(-1).idx
sheet.row(last_index + 1).concat [index, error]
book.write "#{Rails.root}/data/Skipped_Records.xls"
In first run the file is getting created fine.
But after second run, while opening the file LibreOffice says
Unknown or unsupported excel file format.
I am trying to migrate excel data into rails application using rake task. While doing it few rows are skipped because of certain errors. I am trying to log these skipped records.
Please help.

grails rendering pdf plugin not working, simple example?

I am trying to save a simple template to pdf using the rendering plugin, but I cannot get it to work no matter what I try. All I need is for it to save a file within the file system on the server and redirect to a different page.
At the minute the pdf template does not need any parameters as it just prints hello world. Once I get this working I will attempt to add some data.
I am getting errors saying I need to specify a controller if no '/' is appended. But I have tried adding this to no avail. Plus I don't understand which controller it needs as I have tried specifying the controller this action is declared.
Can someone please have a look at this and tell me what I'm doing wrong?
RenderingService pdfRenderingService
def displayPDFSummary = {
ByteArrayOutputStream bytes = pdfRenderingService.render(template: "_pdfTemplate", controller:"RSSCustomerOrder", model: [origSessionId:params.origSessionId])
def fos= new FileOutputStream('NewTestFile.pdf')
fos.write(bytes)
fos.close()
render(template: "_pdfTemplate", params: [origSessionId:params.origSessionId])
}
I am getting the following error messages in the console:
groovy.lang.MissingMethodException: No signature of method: java.io.FileOutputStream.write() is applicable for argument types: (java.io.ByteArrayOutputStream)
(Then prints contents of template...)
Possible solutions: write([B), write(int), write([B), write(int), wait(), wait(long)
Did you look at the FileOutputStream docs? There's no write(OutputStream) method.
Try fos.write(bytes.toByteArray()). Also, bytes.writeTo(fos) may work.

I can't get rails plugin wicked_pdf to work

I wanted to create PDFs for my rails application using wkhtml2pdf and wicked_pdf.
I downloaded and extracted wkhtml2pdf beta 4 and placed it in /usr/local/bin/wkhtml2pdf
I tried running it on a web site and it gave a nice result.
In my rails application (2.3.4) I installed wicked_pdf:
script/plugin install git://github.com/mileszs/wicked_pdf.git
script/generate wicked_pdf
Everything seemed to be ok.
inside script/console I run the following - (with the following output)
wp = WickedPdf.new
=># WickedPdf:0xb62f2c70 #exe_path="/usr/local/bin/wkhtmltopdf"
HTML_DOCUMENT = "<html><body>Hello World</body></html>"
=> "<html><body>Hello World</body></html>"
pdf = wp.pdf_from_string HTML_DOCUMENT
=> "/usr/local/bin/wkhtmltopdf - - -q"
=> "\n\n\n\n\n\n\n\n\n\n"
of course this isn't good. According to the test the result of my last command should start with "%pdf-1.4"
Any idea what I can do?
Having the same problem. Removed the -q option from the wicked_pdf.rb file on line 19 and then was able to get the proper string on the console.
=> "%PDF-1.4\n1 0 obj\n<<\n/Title ...
This also seems to have solved other problems. The PDF still didn't render correctly when using it from the web site - embedded font issue - on to the next issue now.
Hopefully this will work for you.

Resources