Skip to content

Commit

Permalink
Merge pull request #389 from ublefo/fix/pdf-annotation
Browse files Browse the repository at this point in the history
fix: preserve links in PDF
  • Loading branch information
macite authored Aug 3, 2022
2 parents 78cffc5 + 250fd18 commit f489546
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 3 deletions.
11 changes: 11 additions & 0 deletions .ci-setup/newpax.lua.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--- /tmp/texlive/texmf-dist/tex/latex/newpax/newpax.lua
+++ /tmp/texlive/texmf-dist/tex/latex/newpax/newpax.lua
@@ -206,7 +206,7 @@
-- XXXXXX encode/escape the file name?
local function outputENTRY_file (file, pdfedoc)
local bytes = GETSIZE(pdfedoc)
- local date = GETINFO(pdfedoc).CreationDate
+ local date = GETINFO(pdfedoc).CreationDate or "D:22222222222222"
-- file
local a = strENTRY_BEG
a = a .. strCMD_BEG .. constCMD_FILE .. strCMD_END
15 changes: 14 additions & 1 deletion .ci-setup/texlive-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,20 @@ if ! command -v "$TEX_COMPILER" > /dev/null; then

echo "----------------------------------------"
echo "Installing additional texlive packages:"
tlmgr install fontawesome luatextra luacode minted fvextra catchfile xstring framed lastpage
tlmgr install fontawesome luatextra luacode minted fvextra catchfile xstring framed lastpage pdfmanagement-testphase newpax

echo "----------------------------------------"
echo "Patching the newpax package version 0.52 to fix a bug:"
if NEWPAX_VERSION=$(tlmgr info --only-installed --data cat-version newpax) ; then
if [ "$NEWPAX_VERSION" == "0.52" ]; then
echo "Version 0.52 found, patching."
patch -d / -p0 < "${APP_PATH}"/newpax.lua.patch
else
echo "Version $NEWPAX_VERSION found, skipping the patch."
fi
else
echo >&2 "Package newpax not found!"; exit 1;
fi

cd ..

Expand Down
9 changes: 7 additions & 2 deletions app/helpers/file_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,14 @@ def compress_image_to_dest(source, dest, delete_frames = false)

def compress_pdf(path, max_size = 2_500_000)
# trusting path... as it needs to be replaced
logger.debug "Compressing PDF #{path} (#{File.size?(path)} bytes) using GhostScript"
# only compress things over max_size -- defaults to 2.5mb
return if File.size?(path) < max_size
current_filesize = File.size?(path)
if current_filesize < max_size
logger.debug "PDF #{path} (#{current_filesize} bytes) is smaller than #{max_size}, skipping compression."
return
else
logger.debug "Compressing PDF #{path} (#{current_filesize} bytes) using GhostScript"
end

begin
tmp_file = File.join(Dir.tmpdir, 'doubtfire', 'compress', "#{File.dirname(path).split(File::Separator).last}-file.pdf")
Expand Down
6 changes: 6 additions & 0 deletions app/views/layouts/application.pdf.erbtex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<% @latex_config={ :recipe => [ {:command => "lualatex",:runs => 2} ] } %>
\DocumentMetadata{uncompress}
\documentclass[11pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
Expand All @@ -24,6 +25,11 @@
urlcolor=blue,
citecolor=black}

\usepackage{newpax}
\newpaxsetup{usefileattributes=true, addannots=true}
\directlua{require("newpax")}
<%= yield :preamble_newpax %>

\epstopdfDeclareGraphicsRule{.tif}{png}{.png}{convert #1 \OutputFile}
\AppendGraphicsExtensions{.tif}
\epstopdfDeclareGraphicsRule{.tiff}{png}{.png}{convert #1 \OutputFile}
Expand Down
24 changes: 24 additions & 0 deletions app/views/portfolio/portfolio_pdf.pdf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@ end
def task_for_def(td)
task_for_def_id(td.id)
end

# array to hold the list of PDF paths we are inserting for annotation extraction with newpax
document_list = Array.new
%>

\begin{titlepage}
<% if @learning_summary_report %>
% add learning summary report to the document list for annotation extraction
<% document_list.append(@learning_summary_report)%>
\includepdf[addtotoc={\thepage,section,1,Learning Summary Report,chap:learningsummary},pages={1-}]{<%= @learning_summary_report %>}
<% end %>

Expand Down Expand Up @@ -177,6 +182,12 @@ No Tutor
\phantomsection
\label{additional_file_<%= f[:idx] %>}
\addcontentsline{toc}{subsection}{<%= lesc f[:name] %>}

% add additional PDF files to the document list for annotation extraction
<% if f[:kind] == "document" %>
<% document_list.append(@project.portfolio_tmp_file_path(f)) %>
<% end %>

<%= render partial:"shared/file", locals: {idx: f[:idx] - 1, count: @files.count - 1, name: f[:name], base_path: @base_path, file_name: @project.portfolio_tmp_file_path(f), file_type: f[:kind]} %>
<% end %>
<% end %>
Expand Down Expand Up @@ -217,6 +228,19 @@ No Tutor
\end{tabular}
<% end %>
<% if File.exists? task.portfolio_evidence_path %>
% add task evidence to the document list for annotation extraction
<% document_list.append(task.portfolio_evidence_path)%>
\includepdf[pages={1-}]{<%= task.portfolio_evidence_path %>}
<% end %>
<% end %>

% generate lua calls for newpax and insert it in the preamble for annotation extraction
<% content_for :preamble_newpax do %>
\directlua
{
<% document_list.each do |path| %>
% newpax does not allow the .pdf file extension, file paths must be supplied without extension
newpax.writenewpax("<%= File.join(File.dirname(path), File.basename(path, File.extname(path))) %>")
<% end %>
}
<% end %>
16 changes: 16 additions & 0 deletions app/views/task/task_pdf.pdf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ No Tutor % Supervisor's Name

\pagestyle{body}

% array to hold the list of PDF paths we are inserting for annotation extraction with newpax
<% document_list = Array.new %>

<% @files.each_with_index do |file, idx| %>
<% if file != @files.first %>
\clearpage
Expand All @@ -111,8 +114,21 @@ No Tutor % Supervisor's Name
<% end %>

<% if file[:type] == 'document' %>
% add document paths to document_list to generate lua calls to newpax later
<% document_list.append(file[:path])%>
\includepdf[pages={1-}]{<%= file[:path] %>}
<% end %>
<% end %>

% generate lua calls for newpax and insert it in the preamble for annotation extraction
<% content_for :preamble_newpax do %>
\directlua
{
<% document_list.each do |path| %>
% newpax does not allow the .pdf file extension, file paths must be supplied without extension
newpax.writenewpax("<%= File.join(File.dirname(path), File.basename(path, File.extname(path))) %>")
<% end %>
}
<% end %>

% section hello_world (end) Hello World

0 comments on commit f489546

Please sign in to comment.