mirror of https://github.com/captn3m0/muse-dl.git
Add final metadata to PDF
This commit is contained in:
parent
d981000d14
commit
dbf32998a2
|
@ -20,7 +20,9 @@ module Muse::Dl
|
||||||
end
|
end
|
||||||
chapter_ids = thing.chapters.map { |c| c[0] }
|
chapter_ids = thing.chapters.map { |c| c[0] }
|
||||||
pdf_builder = Pdftk.new(parser.tmp)
|
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}"
|
puts "Saved final output to #{parser.output}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
59
src/pdftk.cr
59
src/pdftk.cr
|
@ -37,11 +37,11 @@ module Muse::Dl
|
||||||
output_pdf = File.tempfile("muse-dl-temp", ".pdf")
|
output_pdf = File.tempfile("muse-dl-temp", ".pdf")
|
||||||
bookmark_text_file = File.tempfile("muse-dl-chapter-tmp", ".txt")
|
bookmark_text_file = File.tempfile("muse-dl-chapter-tmp", ".txt")
|
||||||
bookmark_text = <<-END
|
bookmark_text = <<-END
|
||||||
BookmarkBegin
|
BookmarkBegin
|
||||||
BookmarkTitle: #{title}
|
BookmarkTitle: #{title}
|
||||||
BookmarkLevel: 1
|
BookmarkLevel: 1
|
||||||
BookmarkPageNumber: 1
|
BookmarkPageNumber: 1
|
||||||
END
|
END
|
||||||
File.write(bookmark_text_file.path, bookmark_text)
|
File.write(bookmark_text_file.path, bookmark_text)
|
||||||
execute [input_file, "update_info", bookmark_text_file.path, "output", output_pdf.path]
|
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
|
File.rename output_pdf.path, input_file
|
||||||
end
|
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
|
# Do some sanity checks on each Chapter PDF
|
||||||
chapter_ids.each do |id|
|
chapter_ids.each do |id|
|
||||||
raise Muse::Dl::Errors::MissingChapter.new unless File.exists? Fetch.chapter_file_name(id, @tmp_file_path)
|
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
|
# Now let's stitch them together
|
||||||
|
|
||||||
chapter_files = chapter_ids.map { |id| Fetch.chapter_file_name(id, @tmp_file_path) }
|
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
|
execute args
|
||||||
|
|
||||||
# TODO: Validate final file here
|
# TODO: Validate final file here
|
||||||
|
|
||||||
|
return output_file
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue