prawn icon indicating copy to clipboard operation
prawn copied to clipboard

Prawn corrupts links in template file

Open jamieholst opened this issue 13 years ago • 13 comments

Hi,

We're using Prawn to stamp the name of our customers onto our PDF e-book.

The stamped text works fine but all external link destinations in the original PDF (the "template" file) get corrupted in the process. I say "link destinations" because after Prawn stamping the links / clickable areas are still there but they now all point to "null" instead of the original URL – in other words, Prawn isn't removing the links but it is removing / corrupting the destination of them.

Here's the "before Prawn stamping" and "after Prawn stamping":

  • Before, original "template" file where links are working: https://www.dropbox.com/s/gef0bspa36o98ur/template.pdf
  • After, stamped "Prawn" file where links no longer work: https://www.dropbox.com/s/og5u37vu3bo59t2/stamped.pdf

This is the code I use to stamp the original "template.pdf" file:

require 'prawn'

template = File.join(File.dirname(__FILE__), 'template.pdf')
output = File.join(File.dirname(__FILE__), 'stamped.pdf')
licensed_to = "Licensed to Jamie Appleseed"
Prawn::Document.generate(output, template: template) do |pdf|
  pdf.create_stamp("license-stamp") do
    pdf.fill_color "AAAAAA"
    pdf.text_box licensed_to, align: :center, valign: :bottom, size: 7
  end
  pdf.repeat(:all) do
    pdf.stamp("license-stamp")
  end
end

jamieholst avatar Jun 12 '13 13:06 jamieholst

I should note that I've tried this in both Prawn 0.12.0 and 1.0.0.rc2 and both versions generate corrupted links.

jamieholst avatar Jun 12 '13 13:06 jamieholst

I'm seeing this also. @JamieAppleseed did you figure this out?

@sandal did you close this because you want to drop template support?

anthonycreates avatar Mar 10 '14 20:03 anthonycreates

Template support has been dropped since November. It was announced as unsupported in release 0.13.0, disabled by default in 0.14.0, and removed from 0.15.0.

There is an unsupported gem that will give you template support in 0.15+. If anyone wants to attempt a fix to this problem, that's where you should send a pull request: https://github.com/prawnpdf/prawn-templates

But it's very important to keep in mind that the reason why we dropped support for templates is because their implementation is fundamentally flawed, and essentially "unfixable", the code needs to be rewritten to be reliable. None of us with available time to work on Prawn really know how to do that, so unfortunately I don't expect template support to improve in the near future. :frowning:

practicingruby avatar Mar 10 '14 20:03 practicingruby

@anthonycreates Never got it to work I'm afraid. Ended up using PDFtk instead (see http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/).

jamieholst avatar Mar 10 '14 21:03 jamieholst

@JamieAppleseed: Do you have a code sample you can share? This is a really common problem for Prawn users, so if I can make it easier for people who have this problem, I would be happy to at least point them at an example. We can even possibly build a thin wrapper around pdftk and release it as a Prawn extension gem.

practicingruby avatar Mar 10 '14 23:03 practicingruby

@sandal @JamieAppleseed Thanks for the responses. I'm going to check this out today, but really need to figure this out soon. So may switch to pdftk if I need to. @JamieAppleseed Would appreciate any code samples you have from pdftk or info on how you moved over to that library!

anthonycreates avatar Mar 11 '14 13:03 anthonycreates

@anthonycreates: To clarify, PDFTk is not a PDF generator, and so it's not an alternative to Prawn. However, it does allow for adding pages to existing PDFs and doing limited edits like adding watermarks. If your needs are limited to just those things you won't need to use Prawn at all, otherwise you'd be using both.

practicingruby avatar Mar 11 '14 13:03 practicingruby

@sandal Thanks for clarifying that. I am only using the Prawn gem similarly to @JamieAppleseed for watermarking, so I would only need one library.

anthonycreates avatar Mar 11 '14 15:03 anthonycreates

@anthonycreates In that case, definitely look into PDFTk, and if you figure out how to do it, please report back here. We love when people use Prawn, but only when it's going to work better than the other options out there. :grin:

practicingruby avatar Mar 11 '14 15:03 practicingruby

Well I actually still use Prawn to generate a "stamp" file which is then merged onto the main document using PDFtk. The below code should illustrate the basic principle:

stamp = Rails.root.join('some-path-stamp.pdf')
doc = Rails.root.join('some-path-doc.pdf')
output = Rails.root.join('some-path-output.pdf')
Prawn::Document.generate(stamp, page_size: 'A4', bottom_margin: 24) do |pdf|
   # code to generate stamp, for example:
   pdf.text_box "Licensed to #{account.name}", align: :center, valign: :bottom, size: 7
end
system "pdftk #{doc} stamp #{stamp} output #{output}"
FileUtils.rm_rf stamp

jamieholst avatar Mar 11 '14 20:03 jamieholst

Thanks for sharing this example!

practicingruby avatar Mar 12 '14 13:03 practicingruby

Yeah, thanks @JamieAppleseed ! I am going to run this in Heroku. Would you want to see the build pack setup for that?

anthonycreates avatar Mar 12 '14 15:03 anthonycreates

Sure. I think I'll probably write up a wiki page to link to, so any additional examples are welcome.

practicingruby avatar Mar 12 '14 16:03 practicingruby