wicked_pdf
wicked_pdf copied to clipboard
Links refer to internal sections in multiple PDF's are broken
@unixmonkey I'm using wicked_pdf and combine_pdf to make it happen. The output of the result is that links are broken and not working at all.
Monarch-HTML-Importer-PDF-Preview-Full-PDF-1663881297.pdf
@unixmonkey do you know if we can merge multiple PDF's and use links to refer for example: PDF1 -> Table Of contents using the assumption that reference will exists but not at the moment when the file is building but when is combine with the next PDF's. PDF1:
<div id="toc">
<h3>Table of Contents</h3>
<ul>
<li><a href="#text_index_1">Main Heading (h1)</a></li>
<ul>
<li><a href="#Second_level_heading_(h2)">Second level heading (h2)</a></li>
<li><a href="#Another_heading_(h2)">Another heading (h2)</a></li>
<ul>
<li><a href="#Sub_heading_(h3)">Sub heading (h3)</a></li>
</ul>
<li><a href="#One_more_second_level_heading_(h2)">One more second level heading (h2)</a></li>
</ul>
</ul>
</div>
PDF2:
<div id="contents" class="page_break_b">
<div><a name="text_index_1">Main Heading (h1)</a></div>
<div><a name="Another_heading_(h2)">Another heading (h2)</a></div>
<div><a name="Sub_heading_(h3)">Sub heading (h3)</a></div>
<div><a name="One_more_second_level_heading_(h2)">One more second level heading (h2)</a></div>
</div>
The main problem is that the output is not working any link.
def generate_marriott_pdf(original_export)
result = CombinePDF.new
# Table of Contents
@export = ExportTemplate.where(:name => "TOC").first
toc_pdf_html_content = ApplicationController.new.render_to_string( :locals => {:"@export" => @export, :"@project" => @project, :"@firm" => @firm, :"@project_products" => @project_products}, :show_as_html => false, :margin => margin_settings, :pdf => 'marriott-toc.pdf', :layout => "pdf.html.erb", :encoding => "UTF-8", :template => "firm/templates/show.html.erb", :page_size => 'Letter', :javascript_delay => 100 )
toc_src_temp_file = Tempfile.new(['toc_src_temp_file', '.pdf'])
toc_src_temp_file.binmode
toc_src_temp_file.write(toc_pdf_html_content)
toc_src_temp_file.rewind
toc_src_temp_file.close
result << CombinePDF.load(toc_src_temp_file.path, allow_optional_content: true)
count_pages = result.pages.count
result.remove( count_pages - 1 )
# Loop all the intro attachments here
# 1 PDF
@export = ExportTemplate.where(:name => "Title Center").first
title_center_pdf_html_content = ApplicationController.new.render_to_string( :locals => {:"@export" => @export, :"@project" => @project, :"@firm" => @firm, :"@title" => "Test 1", :"@title_index" => "text_index_1"}, :show_as_html => false, :margin => margin_settings, :pdf => 'marriott-title-center.pdf', :layout => "pdf.html.erb", :encoding => "UTF-8", :template => "firm/templates/show.html.erb", :page_size => 'Letter', :javascript_delay => 100 )
title_center_src_temp_file = Tempfile.new(['title_center_src_temp_file', '.pdf'])
title_center_src_temp_file.binmode
title_center_src_temp_file.write(title_center_pdf_html_content)
title_center_src_temp_file.rewind
title_center_src_temp_file.close
result << CombinePDF.load(title_center_src_temp_file.path, allow_optional_content: true)
@export = original_export
temp_file = Tempfile.new([filename_wo_ext, ".#{@export.extension}"])
temp_file.binmode
temp_file.write(result.to_pdf)
temp_file
end
I am not aware of any automated way to rewrite links in a PDF made by combining with a different PDF. Possibly manually with Acrobat, or some way of tagging the links, so that you can make a pass with a different PDF editing software package to replace them?
However it looks like you are creating both PDFs in the code above from HTML, so I think it should work if you created them both together as one PDF.
Wkhtmltopdf (and wicked_pdf) have a way of providing your own table of contents document (search this page for "TOC").
You could also build out your custom TOC html in the same template as your PDF2 content, but separated with the CSS-rule page-break-after: always;
after your TOC content (or page-break-before: always;
before your main content.
If you are using page-numbering, you might need to change to HTML and JS-based, then offset your page number to not count the TOC (may require some math to calculate height of TOC content and how many pages it spans), or be ok with the TOC having page numbers.
Let me know how it goes!