mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Support for writing metadata to PDF files (thanks to John Schember)
This commit is contained in:
commit
ffa6c73c8d
@ -251,6 +251,17 @@ class MOBIMetadataWriter(MetadataWriterPlugin):
|
|||||||
def set_metadata(self, stream, mi, type):
|
def set_metadata(self, stream, mi, type):
|
||||||
from calibre.ebooks.metadata.mobi import set_metadata
|
from calibre.ebooks.metadata.mobi import set_metadata
|
||||||
set_metadata(stream, mi)
|
set_metadata(stream, mi)
|
||||||
|
|
||||||
|
class PDFMetadataWriter(MetadataWriterPlugin):
|
||||||
|
|
||||||
|
name = 'Set PDF metadata'
|
||||||
|
file_types = set(['pdf'])
|
||||||
|
description = _('Set metadata in %s files') % 'PDF'
|
||||||
|
author = 'John Schember'
|
||||||
|
|
||||||
|
def set_metadata(self, stream, mi, type):
|
||||||
|
from calibre.ebooks.metadata.pdf import set_metadata
|
||||||
|
set_metadata(stream, mi)
|
||||||
|
|
||||||
|
|
||||||
plugins = [HTML2ZIP]
|
plugins = [HTML2ZIP]
|
||||||
|
@ -2,10 +2,10 @@ __license__ = 'GPL v3'
|
|||||||
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
|
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||||
'''Read meta information from PDF files'''
|
'''Read meta information from PDF files'''
|
||||||
|
|
||||||
import sys, os, re
|
import sys, os, StringIO
|
||||||
|
|
||||||
from calibre.ebooks.metadata import MetaInformation, authors_to_string, get_parser
|
from calibre.ebooks.metadata import MetaInformation, authors_to_string, get_parser
|
||||||
from pyPdf import PdfFileReader
|
from pyPdf import PdfFileReader, PdfFileWriter
|
||||||
|
|
||||||
def get_metadata(stream):
|
def get_metadata(stream):
|
||||||
""" Return metadata as a L{MetaInfo} object """
|
""" Return metadata as a L{MetaInfo} object """
|
||||||
@ -31,18 +31,27 @@ def get_metadata(stream):
|
|||||||
|
|
||||||
def set_metadata(stream, mi):
|
def set_metadata(stream, mi):
|
||||||
stream.seek(0)
|
stream.seek(0)
|
||||||
raw = stream.read()
|
|
||||||
if mi.title:
|
# Use a StringIO object for the pdf because we will want to over
|
||||||
tit = mi.title.encode('utf-8') if isinstance(mi.title, unicode) else mi.title
|
# write it later and if we are working on the stream directly it
|
||||||
raw = re.compile(r'<<.*?/Title\((.+?)\)', re.DOTALL).sub(lambda m: m.group().replace(m.group(1), tit), raw)
|
# could cause some issues.
|
||||||
if mi.authors:
|
raw = StringIO.StringIO(stream.read())
|
||||||
au = authors_to_string(mi.authors)
|
orig_pdf = PdfFileReader(raw)
|
||||||
if isinstance(au, unicode):
|
|
||||||
au = au.encode('utf-8')
|
title = mi.title if mi.title else orig_pdf.documentInfo.title
|
||||||
raw = re.compile(r'<<.*?/Author\((.+?)\)', re.DOTALL).sub(lambda m: m.group().replace(m.group(1), au), raw)
|
author = authors_to_string(mi.authors) if mi.authors else orig_pdf.documentInfo.author
|
||||||
|
|
||||||
|
out_pdf = PdfFileWriter(title=title, author=author)
|
||||||
|
for page in orig_pdf.pages:
|
||||||
|
out_pdf.addPage(page)
|
||||||
|
|
||||||
|
out_str = StringIO.StringIO()
|
||||||
|
out_pdf.write(out_str)
|
||||||
|
|
||||||
stream.seek(0)
|
stream.seek(0)
|
||||||
stream.truncate()
|
stream.truncate()
|
||||||
stream.write(raw)
|
out_str.seek(0)
|
||||||
|
stream.write(out_str.read())
|
||||||
stream.seek(0)
|
stream.seek(0)
|
||||||
|
|
||||||
def option_parser():
|
def option_parser():
|
||||||
|
Loading…
x
Reference in New Issue
Block a user