Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds ability to specify pdftotext options #31

Merged
merged 3 commits into from
May 8, 2017

Conversation

philgooch
Copy link

  • Adds test for -table option that produces tabular output

* Adds test for `-table` option that produces tabular output
@jonmagic
Copy link
Owner

jonmagic commented May 4, 2017

@philgooch thank you for the PR! I still need to pull it down and try it out but excited to see this 🤘 To be future proof I think I'd like to see options be a hash instead of an array, maybe with a flags key which takes the array of flags. Thoughts on that approach?

* Makes `Page.text()` options a hash where `flags` takes an array of `pdftotext` flags
@philgooch
Copy link
Author

@jonmagic good idea, I've just pushed an update. One thing I've found is that the pdftotext linked to by xpdf is superior to the one linked to by poppler in that it handles end-of-line breaks in the pdf in a more natural way (it wraps them rather than making them hard breaks), and also has the -tables option (poppler has just -layout which is not as accurate for tabular data)

Copy link
Owner

@jonmagic jonmagic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of small changes and then I think this is ready to 🚢

lib/grim/page.rb Outdated
@@ -47,11 +47,11 @@ def save(path, options={})
# pdf[1].text
# # => "This is text from slide 2.\n\nAnd even more text from slide 2."
#
# pdf[1].text(options=["-table"])
# pdf[1].text(options={flags: ["-table"]})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should just be:

#   pdf[1].text({flags: ["-table"]})

lib/grim/page.rb Outdated
# Returns a String.
#
def text(options=[])
command = [@pdftotext_path, "-enc", "UTF-8", "-f", @number, "-l", @number, options.join(", "), Shellwords.escape(@pdf.path), "-"].join(' ')
def text(options={flags: []})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should change to something more like:

def text(options={})
  flags = options.fetch(:flags, [])
  command_parts = [@pdftotext_path, "-enc", "UTF-8", "-f", @number, "-l", @number]
  command_parts += flags if flags.length > 0
  command_parts += [Shellwords.escape(@pdf.path), "-"]
  command = command_parts.join(' ')

@@ -53,7 +53,7 @@

it "should extract tabular data with the -table option" do
pdf = Grim::Pdf.new(fixture_path("table.pdf"))
expect(pdf[0].text(options=["-table"])).to \
expect(pdf[0].text(options={flags: ["-table"]})).to \
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can drop the options= here similar to the comment above.

@philgooch
Copy link
Author

@jonmagic thanks for reviewing the PR, changes made as requested 😸

Copy link
Owner

@jonmagic jonmagic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent ✨

@jonmagic jonmagic merged commit a63a444 into jonmagic:master May 8, 2017
@jonmagic
Copy link
Owner

jonmagic commented May 8, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants