Decompress PDF file in Ruby - ruby

I am making a program that can analyze graphs from PDF files. I pretty much have it working the way I want, and I'm trying to automate the script with AWS Lambda.
On my local machine using mutool does the job, but this doesn't seem to work on AWS.
command = "mutool clean -d #{input_path} #{output_path}"
system command
When I run that in my lambda function the output_path file just remains empty. How can I decompress the PDF file without using mutool/system commands?

gem install pdf-reader
require 'pdf-reader'
reader = PDF::Reader.new(input_path)
content = reader.pages.first.raw_content
File.write(output_path, content)

Related

How to programmatically decompress .tar.xz file (without intermediates) in ruby running on ubuntu 14.04?

SO...
I am writing a ruby script to initialize a production host in order to run an application (create user groups, users, SSH keys, etc.) and I am stuck on installing NPM / Node. I don't want to use a package manager to install it, but rather simply grab the .tar.xz file for the version I want (in this case, 6.9.1) and unpack it to a location I want with the binaries added to my path. Currently, we will get this from Node downloads...
I have found this SO answer that I am using to try and get my script working. It looks like Ruby does not have an out of the box way to handle ".xz" files, but my Ubuntu distribution does have a later version of tar so it can handle "tar -xJf ...". I was thinking to do something along the lines of...
require 'fileutils'
require 'open-uri'
uri = "https://nodejs.org/dist/v6.9.1/node-v6.9.1-linux-x64.tar.xz"
source = open(uri)
data = system("cat", IO.binread(source), "|", "tar", "-xJf", "-")
# put data where I want using FileUtils
...I could definitely get this working with an intermediate file and more system commands (or even just a curl call for that matter, but I am trying to not use system calls where possible). I see there are Gems that I can use such as this, but I would like to not include any dependencies. Any thoughts on achieving an elegant solution?
system is too weak. You want IO::popen for this.
IO.popen(['tar', '-xJC', target_dir, '-f', '-'], 'wb') do |io|
io.write(IO.binread(source))
end
popen will open a subprocess, and give you io connected to its stdin/stdout. No need for FileUtils, -C option tells tar where to put stuff.

How do I alter my gem so that it can run via the command line?

So I am writing a gem that runs through the command line to learn how to count cards. How do I set the file structure so that the file is run and get get input from the user?
For example what runs the function that makes the code work is a ruby file that contains only the following:
require "cardcounter.rb"
CardCounter.run_program
Is there a way that when the user downlaods my cardcounting gem, they can just type cardcount and it would run CardCounter.run_program, without having to be in irb or anything?
You need to create a bin directory in your gem's file structure and put the relevant code there (in your case in cardcount file).
Read more here.

Possible to run zip process on Heroku?

Generating Office docs in OpenXML. Part of the process is using zip to combine directories and files into an archive. This works fine locally
var p = 'cd ' + target + '/; zip -r ../' + this.fname + ' .; cd ..;';
return exec.exec(p, function(err, stdout, stderr) { ... }
But fails on Heroku Cedar, with an error /bin/sh: zip: not found. Logging in via shell (heroku run bash) and running ls /bin, it appears that the zip binary does not exist. gzip does exist, but I think that's different.
Is it possible to run zip on the Heroku from a shell process? From this link below it seems like it should be possible. (That article uses Ruby, I use Node, but I think the shell shouldn't care who's calling it?)
Rails: How can I use system zip on Heroku to make a docx from an xml template?
It says here
How to unzip files in a Heroku Buildpack
that though heroku doesn't include the zip command, the jar command is available.
However, why not use an npm like this one to process your files from within the node app itself:
https://www.npmjs.org/package/zipfile

How to run Gnuplot (.gp) file from Ruby?

I wrote a program in Gnuplot (a .gp file) to read data from a .txt file and plot scatter graph. How to write a program in Ruby env for running the .gp file? Should I put all the files in the same folder as well as the Gnuplot application folder? I'm not sure how to set the right path.
why don't you try the ruby gem "gnuplot"?
sometimes it is easier to use gnuplot directly, but gem gnuplot has helped me a lot too because i can directly put the data that i want to draw from my ruby program.
i recommend you to have a try gem gnuplot
btw, if i'm not mistaken usually gnuplot read data from .dat file instead of .txt

Ruby net/scp, upload file from variable

It seems like net/scp in Ruby (I'm using 1.8.7) only accepts a path and not binary data as "local_file" parameter.
In my case, I have the local file stored in a variable.
Am I required to save->upload->delete a local file, or is it possible to send the file "directly" to the remote server via SSH without temporary creating it locally?
I'm open to other solutions than SCP.
What I tried so far is using normal SSH and then executing
echo 'binary here' > remote_file_name
however I'm concerned about command length limits in Unix and I faced escaping problems and so forth...
While it will interpret a string as a file name, it should recognise a StringIO object as actual data to upload.

Resources