prawn icon indicating copy to clipboard operation
prawn copied to clipboard

Internal PDF links

Open 8vius opened this issue 6 years ago • 4 comments

I came across this article: https://afterthoughtsoftware.com/posts/ruby-how-to-add-internal-pdf-links-inside-a-table-using-prawn-and-prawn-table it seems the add_dest with dest_xyz works for this purpose I was wondering if anyone would have an idea of how to link to a specific page?

In my case I'm building a PDF and then adding other PDFs to it using CombinePDF and I'd like to be able to link to the pages that I append.

8vius avatar Jan 22 '19 18:01 8vius

This won't be possible because you can only link to existing pages (PDF destinations require a page object, not a page number).

You would need to combine the other PDFs and then add the content. Then you would have the page objects available for linking.

It might be possible with "Remote GoTo Actions" (PDF 1.7 spec 12.6.4.3) but I'm not sure.

gettalong avatar Feb 09 '19 22:02 gettalong

@8vius have you been able to solve it? I have the same problem, the difference I'm using HexaPdf to combine it.

manoelneto avatar Mar 04 '20 16:03 manoelneto

@manoelneto Even if you use HexaPDF for the combining part, you won't be able to do it unless you first append the PDFs to an empty PDF document, then create the pages you need with HexaPDF and link to the appended page objects. The thing is, though, that you would also need to use HexaPDF for the page creation part.

gettalong avatar Mar 04 '20 23:03 gettalong

Just stumbled into this issue wanting to generate a ToC inside the prawn on arbitrary page. It seemed nearly impossible but turns out can be done quite easily with:

# anchor - String
# page - Integer, zero start
def add_anchor(anchor, page)
    destination = dest_xyz(bounds.absolute_left, bounds.absolute_top, nil, page)
    add_dest(anchor, destination)
end

# Each page should add anchor and add itself to outline
def page(code, title, page_number)
   add_anchor(code, page_number)
   outline.page(title: title, destination: page_number + 1) # Outline seems to number pages starting with 1

   render_page
end

def title_page
    text "<link anchor='#{code}'>#{title}</link>", size: 20, align: :left, inline_format: true
end

Note that title page can be rendered first as long as you know the structure you're expecting or can be rendered last by using move_to_page

swistak avatar Sep 04 '22 20:09 swistak