This commit is contained in:
Kovid Goyal 2012-08-26 13:41:10 +05:30
parent 37a38b606c
commit 10e0c2bead
3 changed files with 17 additions and 16 deletions

View File

@ -13,8 +13,9 @@ import os, sys
from calibre.utils.config import OptionParser from calibre.utils.config import OptionParser
from calibre.utils.logging import Log from calibre.utils.logging import Log
from calibre.constants import preferred_encoding, plugins from calibre.constants import preferred_encoding
from calibre.ebooks.pdf.verify import is_valid_pdfs, is_encrypted from calibre.ebooks.pdf.verify import is_valid_pdfs, is_encrypted
from calibre.utils.podofo import get_podofo
from calibre import prints from calibre import prints
USAGE = '\n%prog %%name ' + _('''\ USAGE = '\n%prog %%name ' + _('''\
@ -32,9 +33,7 @@ def option_parser(name):
return OptionParser(usage=usage) return OptionParser(usage=usage)
def print_info(pdf_path): def print_info(pdf_path):
podofo, podofo_err = plugins['podofo'] podofo = get_podofo()
if not podofo:
raise RuntimeError('Failed to load PoDoFo with error:'+podofo_err)
p = podofo.PDFDoc() p = podofo.PDFDoc()
p.open(pdf_path) p.open(pdf_path)

View File

@ -13,6 +13,12 @@ from calibre.ebooks.metadata import authors_to_string
from calibre.ptempfile import TemporaryDirectory from calibre.ptempfile import TemporaryDirectory
from calibre.utils.ipc.simple_worker import fork_job, WorkerError from calibre.utils.ipc.simple_worker import fork_job, WorkerError
def get_podofo():
podofo, podofo_err = plugins['podofo']
if podofo is None:
raise RuntimeError('Failed to load podofo: %s'%podofo_err)
return podofo
def prep(val): def prep(val):
if not val: if not val:
return u'' return u''
@ -41,10 +47,7 @@ def set_metadata(stream, mi):
stream.seek(0) stream.seek(0)
def set_metadata_(tdir, title, authors, bkp, tags): def set_metadata_(tdir, title, authors, bkp, tags):
podofo, podofo_err = plugins['podofo'] podofo = get_podofo()
if podofo is None:
raise RuntimeError('Failed to load podofo: %s'%podofo_err)
os.chdir(tdir) os.chdir(tdir)
p = podofo.PDFDoc() p = podofo.PDFDoc()
p.open(u'input.pdf') p.open(u'input.pdf')
@ -80,10 +83,7 @@ def set_metadata_(tdir, title, authors, bkp, tags):
def delete_all_but(path, pages): def delete_all_but(path, pages):
''' Delete all the pages in the pdf except for the specified ones. Negative ''' Delete all the pages in the pdf except for the specified ones. Negative
numbers are counted from the end of the PDF. ''' numbers are counted from the end of the PDF. '''
podofo, podofo_err = plugins['podofo'] podofo = get_podofo()
if podofo is None:
raise RuntimeError('Failed to load podofo: %s'%podofo_err)
p = podofo.PDFDoc() p = podofo.PDFDoc()
with open(path, 'rb') as f: with open(path, 'rb') as f:
raw = f.read() raw = f.read()

View File

@ -88,18 +88,20 @@ PDFDoc_save(PDFDoc *self, PyObject *args, PyObject *kwargs) {
static PyObject * static PyObject *
PDFDoc_write(PDFDoc *self, PyObject *args, PyObject *kwargs) { PDFDoc_write(PDFDoc *self, PyObject *args, PyObject *kwargs) {
PyObject *ans; PyObject *ans;
PdfRefCountedBuffer buffer(1*1024*1024);
PdfOutputDevice out(&buffer);
try { try {
PdfRefCountedBuffer buffer(1*1024*1024);
PdfOutputDevice out(&buffer);
self->doc->Write(&out); self->doc->Write(&out);
ans = PyBytes_FromStringAndSize(buffer.GetBuffer(), out.Tell());
if (ans == NULL) PyErr_NoMemory();
} catch(const PdfError &err) { } catch(const PdfError &err) {
podofo_set_exception(err); podofo_set_exception(err);
return NULL; return NULL;
} catch (...) {
return PyErr_NoMemory();
} }
ans = PyBytes_FromStringAndSize(buffer.GetBuffer(), out.Tell());
if (ans == NULL) PyErr_NoMemory();
return ans; return ans;
} }
// }}} // }}}