Add final metadata to PDF

This commit is contained in:
Nemo 2020-03-29 18:51:07 +05:30
parent d981000d14
commit dbf32998a2
2 changed files with 55 additions and 8 deletions

View File

@ -20,7 +20,9 @@ module Muse::Dl
end
chapter_ids = thing.chapters.map { |c| c[0] }
pdf_builder = Pdftk.new(parser.tmp)
pdf_builder.stitch(parser.output, chapter_ids)
temp_stitched_file = pdf_builder.stitch chapter_ids
pdf_builder.add_metadata(temp_stitched_file, parser.output, thing)
temp_stitched_file.delete
puts "Saved final output to #{parser.output}"
end
end

View File

@ -37,11 +37,11 @@ module Muse::Dl
output_pdf = File.tempfile("muse-dl-temp", ".pdf")
bookmark_text_file = File.tempfile("muse-dl-chapter-tmp", ".txt")
bookmark_text = <<-END
BookmarkBegin
BookmarkTitle: #{title}
BookmarkLevel: 1
BookmarkPageNumber: 1
END
BookmarkBegin
BookmarkTitle: #{title}
BookmarkLevel: 1
BookmarkPageNumber: 1
END
File.write(bookmark_text_file.path, bookmark_text)
execute [input_file, "update_info", bookmark_text_file.path, "output", output_pdf.path]
@ -50,7 +50,50 @@ END
File.rename output_pdf.path, input_file
end
def stitch(output_file : String, chapter_ids : Array(String))
def add_metadata(input_file : File, output_file : String, book : Book)
# First we have to dump the current metadata
metadata_text_file = File.tempfile("muse-dl-metadata-tmp", ".txt")
# TODO: Add version info in the Creator/Producer
text = <<-EOT
InfoBegin
InfoKey: Creator
InfoValue: Project MUSE (https://muse.jhu.edu/)
InfoBegin
InfoKey: Producer
InfoValue: Muse-DL
InfoBegin
InfoKey: Title
InfoValue: #{book.title}
InfoBegin
InfoKey: Keywords
InfoValue: Publisher: #{book.publisher}, Published #{book.date}
InfoBegin
InfoKey: Author
InfoValue: #{book.author}
InfoBegin
InfoKey: Subject
InfoValue: #{book.summary.gsub("\n", " ")}
EOT
# Known Info keys, if they are present
["ISBN", "Related ISBN", "DOI", "Language", "OCLC"].each do |label|
if book.info.has_key? label
text += <<-EOT
InfoBegin
InfoKey: #{label}
InfoValue: #{book.info[label]}
EOT
end
end
File.write(metadata_text_file.path, text)
execute [input_file.path, "update_info_utf8", metadata_text_file.path, "output", output_file]
metadata_text_file.delete
end
def stitch(chapter_ids : Array(String))
output_file = File.tempfile("muse-dl-stitched-tmp", ".pdf")
# Do some sanity checks on each Chapter PDF
chapter_ids.each do |id|
raise Muse::Dl::Errors::MissingChapter.new unless File.exists? Fetch.chapter_file_name(id, @tmp_file_path)
@ -60,10 +103,12 @@ END
# Now let's stitch them together
chapter_files = chapter_ids.map { |id| Fetch.chapter_file_name(id, @tmp_file_path) }
args = chapter_files + ["cat", "output", output_file]
args = chapter_files + ["cat", "output", output_file.path]
execute args
# TODO: Validate final file here
return output_file
end
end
end