Skip to content

Tag Archive for plugin

Plugin Update: PDF Bookmark bug fix and French translation

If you are having problems with the bookmark plugin not working for some documents, this update (1.2) should fix that. The update also adds a French translation to the Bookmark menu and provides an installation path for Linux users. Thanks to TitCouille for help with all of those items.

The plugin should work correctly for Adobe Reader 7, 8 and 9. If you’re interested in helping add a translation for another language please let me know. There are currently only 3 items that need translating – “Bookmark”, “Bookmark This Page” and “Go To Bookmark”.

See the full details and download the plugin.

Plugin Update: PDF Bookmark for Adobe Reader 8

Some of you may already be using the PDF bookmark plugin I put together for Adobe Reader. It provides a quick and easy way to bookmark the page your on and return back to that spot later. It’s especially handy when reading large PDF books.

Thanks to the input of Michael Hartl, I updated the plugin to work properly with Adobe Reader 8. Adobe turned on some security settings by default in the new Adobe Reader which broke the previous version of the plugin.

See the full details and download the plugin.

Moment of Open Source Zen

Every day, as a developer, I’m grateful for the thousands of programmers around the world who have so graciously shared their time, knowledge, and programming code. I have access to millions of lines of free code at any moment through the Internet which allows me to write programs more easily, learn new concepts, and make a difference in society.

Yes, every open source developer plays their part in making life better. Take, for example, the recent updates to Sean Coon’s blog. A few tweaks to some great free programming code and Sean’s rapidly spreading the word about The People, Yes which directly engages the homeless community to share their voice through blogging. He’s able to send a text message which is then automatically relayed to his friends/acquaintances (through Twitter) and also posted to his blog (using a modified version of Alex King’s Twitter Tools plugin). Don’t forget that in conjunction with this great plugin there are other pieces of the free code puzzle – the powerful blogging tool, the simple programming language, and the server’s solid operating system. All of this was made possible by many generous programmers sharing their time and skills.

So here’s to the programming language, plugin, framework, and code snippet givers around the globe. You truly make a difference in the world.

Execute Rails Code Before the View is Rendered

At one time, I was looking for a way to execute code for every action in my controller before the view was rendered. I saw this come up again yesterday in the #rubyonrails IRC channel so I looked at it a bit more. Currently, Rails provides a before_filter method which will “run before actions on this controller are performed” or an after_filter which will “run after actions on this controller are performed.” What I wanted was a before_render filter which would run right at the end of the action but before render/view was executed.

Here is one idea how to do this. Since you want the code to run right before render is executed just override the render method that’s in ActionController. You can put the following code inside any of your individual controllers or add it to your ApplicationController and it will run in all of your controllers.

protected
  def render(options = nil, deprecated_status = nil, &block)
    # your code goes here
    @rockon = "rock and roll!"

    # call the ActionController::Base render to show the page
    super
  end

This is just an example, and maybe someone will take this a step further and actually create a before_render filter plugin. It would be nice to have the same flexibility as before_filter and after_filter which allows you to include “:only” or “:exclude” certain actions.

Writing a Custom FormBuilder in Rails

I’m currently working on a Ruby on Rails application where I need lots of text fields that have the same properties. I decided to override text_field and have it output all of the extra attribues automatically. This helps keep the view code cleaner, lets me change all of these text fields in one place, and also helps me avoid typing mistakes (at least in this section of the code). Thanks to Ruby on Rails core team member Rick Olson (aka technoweenie) for pointing me to his LabeledFormHelper plugin, which taught me this technique.

Before the custom FormBuilder my initial view code looked something like:

<% form_for(:spreadsheet, @spreadsheet, :url => { :action => 'create' }) do |s| %>

    <% fields_for :numbers, @spreadsheet.numbers do |f| %>

      <%= f.text_field :field1, :onkeypress => 'return isNumberKey(event);', :maxlength => 3 %>
      <%= f.text_field :field2, :onkeypress => 'return isNumberKey(event);', :maxlength => 3 %>

      ... and so on

    <% end %>
<% end %>

So I created a custom FormBuilder in my helper to do the extra text_field work for me:

module SpeadsheetsHelper

  # tell all of these methods to use my custom FormBuilder
  [:form_for, :fields_for, :form_remote_for, :remote_form_for].each do |meth|
    src = <<-end_src
      def speadsheets_#{meth}(object_name, *args, &proc)
        options = args.last.is_a?(Hash) ? args.pop : {}
        options.update(:builder => SpeadsheetsFormBuilder)
        #{meth}(object_name, *(args << options), &proc)
      end
    end_src
    module_eval src, __FILE__, __LINE__
  end

  # the custom FormBuilder
  class SpeadsheetsFormBuilder < ActionView::Helpers::FormBuilder    

    # add onkeypress and set maxlength of field to 3 to all text fields
    def text_field(method, options={})
      super(method, options.merge(:onkeypress => 'return isNumberKey(event);', :maxlength => 3))
    end

  end
end

Now my view code is much simpler and helps avoid goofups:

<% spreadsheets_form_for(:spreadsheet, @spreadsheet, :url => { :action => 'create' }) do |s| %>

    <% spreadsheets_fields_for :numbers, @spreadsheet.numbers do |f| %>

      <%= f.text_field :field1 %>
      <%= f.text_field :field2 %>

      ... and so on

    <% end %>
<% end %>

Notice in the view that instead of using form_for and fields_for you need to use the custom methods spreadsheets_form_for and spreadsheets_fields_for which were created in SpeadsheetsHelper.

Plugin: Bookmark A Page In Your PDF

With the long, yet wonderful, books in PDF format these days (Agile Web Development with Rails by Dave Thomas and David Heinemeier Hansson with Leon Breedt, Mike Clark, Thomas Fuchs, and Andreas Schwarz [570 pages]; Programming Ruby by Dave Thomas, with Chad Fowler and Andy Hunt [864 pages]) there needs to be a better way to digitally bookmark where you left off. For some reason Adobe Reader (Acrobat Reader) doesn’t have this capability built in. I found a plugin for Acrobat Reader to do multiple bookmarks per document from PDF Hacks. However, I really just want it to be simple like a physical book – you bookmark the page you’re on when you stop and go to that page when you pick back up. So here is my plugin to bookmark your page in Adobe Reader.

Download:

PDF Bookmark 1.2

Change Log:

  • 1.2 – Nov 30, 2008 – Use documentFileName on PDFs that don’t have a docID. Add French translation (TitCouille). Add installation paths for Linux (TitCouille).
  • 1.1 – Dec 20, 2007 – Update to work with Adobe Reader 8 due to security changes (app.trustedFunction)
  • 1.0 – Sep 15, 2006 – Initial release

Installation:

Adobe Reader 9 – copy pdf_bookmark.js to:

  • Mac: ~/Library/Application Support/Adobe/Acrobat/9.0_x86/JavaScripts (or similar)
  • Linux: /home/[User Name]/.adobe/Acrobat/9.0/JavaScripts/
  • Windows XP: C:\Documents and Settings\[User Name]\Application Data\Adobe\Acrobat\9.0\JavaScripts (or similar)
  • Windows 7 & Vista: C:\Users\[User Name]\AppData\Roaming\Adobe\Acrobat\9.0\JavaScripts

Adobe Reader 8 – copy pdf_bookmark.js to:

  • Mac: ~/Library/Acrobat User Data/8.0_x86/JavaScripts (or similar)
  • Linux: /home/[User Name]/.adobe/Acrobat/8.0/JavaScripts/
  • Windows: C:\Documents and Settings\[User Name]\Application Data\Adobe\Acrobat\8.0\JavaScripts (or similar)

Adobe Reader 7 – copy pdf_bookmark.js to:

  • Mac: ~/Library/Acrobat User Data/7.0/JavaScripts (or similar)
  • Linux: /home/[User Name]/.adobe/Acrobat/7.0/JavaScripts/
  • Windows: C:\Documents and Settings\[User Name]\Application Data\Adobe\Acrobat\7.0\JavaScripts (or similar)

Note: you may have to create the JavaScripts folder if it doesn’t exist. Thanks for all of the comments and emails about working systems and folder!

Usage:

Open your document in Adobe Reader. Then choose Tools -> Bookmark from the menu. “Bookmark This Page” adds the bookmark. “Go To Bookmark” sends you to the bookmark you created. It remembers which PDF document you bookmarked so it works on all of your PDFs simultaneously. (Tested with Acrobat Reader 7, Adobe Reader 8 (thanks Michael) and Adobe Reader 9 on Mac OS X, Linux (thanks TitCouille) and Windows)

PDF Bookmark Plugin Screenshot (8.0)