本文整理汇总了Python中calibre.ebooks.pdf.render.engine.PdfDevice.add_outline方法的典型用法代码示例。如果您正苦于以下问题:Python PdfDevice.add_outline方法的具体用法?Python PdfDevice.add_outline怎么用?Python PdfDevice.add_outline使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类calibre.ebooks.pdf.render.engine.PdfDevice
的用法示例。
在下文中一共展示了PdfDevice.add_outline方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ImagePDFWriter
# 需要导入模块: from calibre.ebooks.pdf.render.engine import PdfDevice [as 别名]
# 或者: from calibre.ebooks.pdf.render.engine.PdfDevice import add_outline [as 别名]
class ImagePDFWriter(object):
def __init__(self, opts, log, cover_data=None, toc=None):
from calibre.gui2 import must_use_qt
must_use_qt()
self.logger = self.log = log
self.opts = opts
self.cover_data = cover_data
self.toc = toc
def dump(self, items, out_stream, pdf_metadata):
opts = self.opts
page_size = get_page_size(self.opts)
ml, mr = opts.margin_left, opts.margin_right
self.doc = PdfDevice(
out_stream, page_size=page_size, left_margin=ml,
top_margin=opts.margin_top, right_margin=mr,
bottom_margin=opts.margin_bottom,
errors=self.log.error, debug=self.log.debug, compress=not
opts.uncompressed_pdf, opts=opts, mark_links=opts.pdf_mark_links)
self.painter = QPainter(self.doc)
self.doc.set_metadata(title=pdf_metadata.title,
author=pdf_metadata.author,
tags=pdf_metadata.tags, mi=pdf_metadata.mi)
self.doc_title = pdf_metadata.title
self.doc_author = pdf_metadata.author
page_rect = QRect(*self.doc.full_page_rect)
for imgpath in items:
self.log.debug('Processing %s...' % imgpath)
self.doc.init_page()
p = QPixmap()
with lopen(imgpath, 'rb') as f:
if not p.loadFromData(f.read()):
raise ValueError('Could not read image from: {}'.format(imgpath))
draw_image_page(page_rect,
self.painter, p,
preserve_aspect_ratio=True)
self.doc.end_page()
if self.toc is not None and len(self.toc) > 0:
self.doc.add_outline(self.toc)
self.painter.end()
if self.doc.errors_occurred:
raise Exception('PDF Output failed, see log for details')
示例2: PDFWriter
# 需要导入模块: from calibre.ebooks.pdf.render.engine import PdfDevice [as 别名]
# 或者: from calibre.ebooks.pdf.render.engine.PdfDevice import add_outline [as 别名]
#.........这里部分代码省略.........
mt, mb = map(self.doc.to_px, (opts.margin_top, opts.margin_bottom))
self.margin_top, self.margin_bottom = map(lambda x:int(floor(x)), (mt, mb))
self.painter = QPainter(self.doc)
self.doc.set_metadata(title=pdf_metadata.title,
author=pdf_metadata.author,
tags=pdf_metadata.tags, mi=pdf_metadata.mi)
self.doc_title = pdf_metadata.title
self.doc_author = pdf_metadata.author
self.painter.save()
try:
if self.cover_data is not None:
p = QPixmap()
try:
p.loadFromData(self.cover_data)
except TypeError:
self.log.warn('This ebook does not have a raster cover, cannot generate cover for PDF'
'. Cover type: %s' % type(self.cover_data))
if not p.isNull():
self.doc.init_page()
draw_image_page(QRect(*self.doc.full_page_rect),
self.painter, p,
preserve_aspect_ratio=self.opts.preserve_cover_aspect_ratio)
self.doc.end_page()
finally:
self.painter.restore()
QTimer.singleShot(0, self.render_book)
if self.loop.exec_() == 1:
raise Exception('PDF Output failed, see log for details')
if self.toc is not None and len(self.toc) > 0:
self.doc.add_outline(self.toc)
self.painter.end()
if self.doc.errors_occurred:
raise Exception('PDF Output failed, see log for details')
def render_inline_toc(self):
self.rendered_inline_toc = True
from calibre.ebooks.pdf.render.toc import toc_as_html
raw = toc_as_html(self.toc, self.doc, self.opts)
pt = PersistentTemporaryFile('_pdf_itoc.htm')
pt.write(raw)
pt.close()
self.render_queue.append(pt.name)
self.render_next()
def render_book(self):
if self.doc.errors_occurred:
return self.loop.exit(1)
try:
if not self.render_queue:
if self.opts.pdf_add_toc and self.toc is not None and len(self.toc) > 0 and not hasattr(self, 'rendered_inline_toc'):
return self.render_inline_toc()
self.loop.exit()
else:
self.render_next()
except:
self.logger.exception('Rendering failed')
self.loop.exit(1)
def render_next(self):
item = unicode(self.render_queue.pop(0))
示例3: PDFWriter
# 需要导入模块: from calibre.ebooks.pdf.render.engine import PdfDevice [as 别名]
# 或者: from calibre.ebooks.pdf.render.engine.PdfDevice import add_outline [as 别名]
#.........这里部分代码省略.........
self.painter = QPainter(self.doc)
try:
self.book_language = pdf_metadata.mi.languages[0]
except Exception:
self.book_language = 'eng'
self.doc.set_metadata(title=pdf_metadata.title,
author=pdf_metadata.author,
tags=pdf_metadata.tags, mi=pdf_metadata.mi)
self.doc_title = pdf_metadata.title
self.doc_author = pdf_metadata.author
self.painter.save()
try:
if self.cover_data is not None:
p = QPixmap()
try:
p.loadFromData(self.cover_data)
except TypeError:
self.log.warn('This ebook does not have a raster cover, cannot generate cover for PDF'
'. Cover type: %s' % type(self.cover_data))
if not p.isNull():
self.doc.init_page()
draw_image_page(QRect(*self.doc.full_page_rect),
self.painter, p,
preserve_aspect_ratio=self.opts.preserve_cover_aspect_ratio)
self.doc.end_page()
finally:
self.painter.restore()
QTimer.singleShot(0, self.render_book)
if self.loop.exec_() == 1:
raise Exception('PDF Output failed, see log for details')
if self.toc is not None and len(self.toc) > 0:
self.doc.add_outline(self.toc)
self.painter.end()
if self.doc.errors_occurred:
raise Exception('PDF Output failed, see log for details')
def render_inline_toc(self):
evaljs = self.view.page().mainFrame().evaluateJavaScript
self.rendered_inline_toc = True
from calibre.ebooks.pdf.render.toc import toc_as_html
raw = toc_as_html(self.toc, self.doc, self.opts, evaljs)
pt = PersistentTemporaryFile('_pdf_itoc.htm')
pt.write(raw)
pt.close()
self.render_queue.append(pt.name)
self.render_next()
def render_book(self):
if self.doc.errors_occurred:
return self.loop.exit(1)
try:
if not self.render_queue:
if self.opts.pdf_add_toc and self.toc is not None and len(self.toc) > 0 and not hasattr(self, 'rendered_inline_toc'):
return self.render_inline_toc()
self.loop.exit()
else:
self.render_next()
except:
self.logger.exception('Rendering failed')
self.loop.exit(1)
def render_next(self):
示例4: PDFWriter
# 需要导入模块: from calibre.ebooks.pdf.render.engine import PdfDevice [as 别名]
# 或者: from calibre.ebooks.pdf.render.engine.PdfDevice import add_outline [as 别名]
class PDFWriter(QObject):
def _pass_json_value_getter(self):
val = json.dumps(self.bridge_value)
return QString(val)
def _pass_json_value_setter(self, value):
self.bridge_value = json.loads(unicode(value))
_pass_json_value = pyqtProperty(QString, fget=_pass_json_value_getter,
fset=_pass_json_value_setter)
def __init__(self, opts, log, cover_data=None, toc=None):
from calibre.gui2 import is_ok_to_use_qt
if not is_ok_to_use_qt():
raise Exception('Not OK to use Qt')
QObject.__init__(self)
self.logger = self.log = log
self.opts = opts
self.cover_data = cover_data
self.paged_js = None
self.toc = toc
self.loop = QEventLoop()
self.view = QWebView()
self.page = Page(opts, self.log)
self.view.setPage(self.page)
self.view.setRenderHints(QPainter.Antialiasing|
QPainter.TextAntialiasing|QPainter.SmoothPixmapTransform)
self.view.loadFinished.connect(self.render_html,
type=Qt.QueuedConnection)
for x in (Qt.Horizontal, Qt.Vertical):
self.view.page().mainFrame().setScrollBarPolicy(x,
Qt.ScrollBarAlwaysOff)
self.report_progress = lambda x, y: x
def dump(self, items, out_stream, pdf_metadata):
opts = self.opts
page_size = get_page_size(self.opts)
xdpi, ydpi = self.view.logicalDpiX(), self.view.logicalDpiY()
# We cannot set the side margins in the webview as there is no right
# margin for the last page (the margins are implemented with
# -webkit-column-gap)
ml, mr = opts.margin_left, opts.margin_right
self.doc = PdfDevice(out_stream, page_size=page_size, left_margin=ml,
top_margin=0, right_margin=mr, bottom_margin=0,
xdpi=xdpi, ydpi=ydpi, errors=self.log.error,
debug=self.log.debug, compress=not
opts.uncompressed_pdf,
mark_links=opts.pdf_mark_links)
self.footer = opts.pdf_footer_template
if self.footer is None and opts.pdf_page_numbers:
self.footer = '<p style="text-align:center; text-indent: 0">_PAGENUM_</p>'
self.header = opts.pdf_header_template
min_margin = 36
if self.footer and opts.margin_bottom < min_margin:
self.log.warn('Bottom margin is too small for footer, increasing it.')
opts.margin_bottom = min_margin
if self.header and opts.margin_top < min_margin:
self.log.warn('Top margin is too small for header, increasing it.')
opts.margin_top = min_margin
self.page.setViewportSize(QSize(self.doc.width(), self.doc.height()))
self.render_queue = items
self.total_items = len(items)
mt, mb = map(self.doc.to_px, (opts.margin_top, opts.margin_bottom))
self.margin_top, self.margin_bottom = map(lambda x:int(floor(x)), (mt, mb))
self.painter = QPainter(self.doc)
self.doc.set_metadata(title=pdf_metadata.title,
author=pdf_metadata.author,
tags=pdf_metadata.tags)
self.painter.save()
try:
if self.cover_data is not None:
p = QPixmap()
p.loadFromData(self.cover_data)
if not p.isNull():
self.doc.init_page()
draw_image_page(QRect(0, 0, self.doc.width(), self.doc.height()),
self.painter, p,
preserve_aspect_ratio=self.opts.preserve_cover_aspect_ratio)
self.doc.end_page()
finally:
self.painter.restore()
QTimer.singleShot(0, self.render_book)
if self.loop.exec_() == 1:
raise Exception('PDF Output failed, see log for details')
if self.toc is not None and len(self.toc) > 0:
self.doc.add_outline(self.toc)
self.painter.end()
if self.doc.errors_occurred:
raise Exception('PDF Output failed, see log for details')
#.........这里部分代码省略.........
示例5: PDFWriter
# 需要导入模块: from calibre.ebooks.pdf.render.engine import PdfDevice [as 别名]
# 或者: from calibre.ebooks.pdf.render.engine.PdfDevice import add_outline [as 别名]
#.........这里部分代码省略.........
self.page.setViewportSize(QSize(self.doc.width(), self.doc.height()))
self.render_queue = items
self.total_items = len(items)
mt, mb = map(self.doc.to_px, (opts.margin_top, opts.margin_bottom))
self.margin_top, self.margin_bottom = map(lambda x: int(floor(x)), (mt, mb))
self.painter = QPainter(self.doc)
self.doc.set_metadata(title=pdf_metadata.title, author=pdf_metadata.author, tags=pdf_metadata.tags)
self.doc_title = pdf_metadata.title
self.doc_author = pdf_metadata.author
self.painter.save()
try:
if self.cover_data is not None:
p = QPixmap()
p.loadFromData(self.cover_data)
if not p.isNull():
self.doc.init_page()
draw_image_page(
QRect(*self.doc.full_page_rect),
self.painter,
p,
preserve_aspect_ratio=self.opts.preserve_cover_aspect_ratio,
)
self.doc.end_page()
finally:
self.painter.restore()
QTimer.singleShot(0, self.render_book)
if self.loop.exec_() == 1:
raise Exception("PDF Output failed, see log for details")
if self.toc is not None and len(self.toc) > 0:
self.doc.add_outline(self.toc)
self.painter.end()
if self.doc.errors_occurred:
raise Exception("PDF Output failed, see log for details")
def render_inline_toc(self):
self.rendered_inline_toc = True
from calibre.ebooks.pdf.render.toc import toc_as_html
raw = toc_as_html(self.toc, self.doc, self.opts)
pt = PersistentTemporaryFile("_pdf_itoc.htm")
pt.write(raw)
pt.close()
self.render_queue.append(pt.name)
self.render_next()
def render_book(self):
if self.doc.errors_occurred:
return self.loop.exit(1)
try:
if not self.render_queue:
if (
self.opts.pdf_add_toc
and self.toc is not None
and len(self.toc) > 0
and not hasattr(self, "rendered_inline_toc")
):
return self.render_inline_toc()
self.loop.exit()
else:
self.render_next()