def get_page_size(opts, for_comic=False): use_profile = opts.use_profile_size and opts.output_profile.short_name != 'default' and opts.output_profile.width <= 9999 if use_profile: w = (opts.output_profile.comic_screen_size[0] if for_comic else opts.output_profile.width) h = (opts.output_profile.comic_screen_size[1] if for_comic else opts.output_profile.height) dpi = opts.output_profile.dpi factor = 72.0 / dpi page_size = QPageSize(QSizeF(factor * w, factor * h), QPageSize.Point, matchPolicy=QPageSize.ExactMatch) else: page_size = None if opts.custom_size is not None: page_size = parse_pdf_page_size(opts.custom_size, opts.unit, opts.output_profile.dpi) if page_size is None: page_size = QPageSize(getattr(QPageSize, opts.paper_size.capitalize())) return page_size
def initialize(self, choices=None): from calibre.utils.icu import numeric_sort_key if self.system_default_paper_size is None: PaperSizes.system_default_paper_size = 'a4' if iswindows or ismacos: # On Linux, this can cause Qt to load the system cups plugin # which can crash: https://bugs.launchpad.net/calibre/+bug/1861741 PaperSizes.system_default_paper_size = 'letter' if QPrinter().pageSize() == QPagedPaintDevice.Letter else 'a4' if not choices: from calibre.ebooks.conversion.plugins.pdf_output import PAPER_SIZES choices = PAPER_SIZES for a in sorted(choices, key=numeric_sort_key): s = getattr(QPageSize, a.capitalize()) sz = QPageSize.definitionSize(s) unit = {QPageSize.Millimeter: 'mm', QPageSize.Inch: 'inch'}[QPageSize.definitionUnits(s)] name = '{} ({:g} x {:g} {})'.format(QPageSize.name(s), sz.width(), sz.height(), unit) self.addItem(name, a)
def initialize(self, choices=None): from calibre.utils.icu import numeric_sort_key if self.system_default_paper_size is None: QComboBox.system_default_paper_size = 'letter' if QPrinter( ).pageSize() == QPagedPaintDevice.Letter else 'a4' if not choices: from calibre.ebooks.conversion.plugins.pdf_output import PAPER_SIZES choices = PAPER_SIZES for a in sorted(choices, key=numeric_sort_key): s = getattr(QPageSize, a.capitalize()) sz = QPageSize.definitionSize(s) unit = { QPageSize.Millimeter: 'mm', QPageSize.Inch: 'inch' }[QPageSize.definitionUnits(s)] name = '{} ({:g} x {:g} {})'.format(QPageSize.name(s), sz.width(), sz.height(), unit) self.addItem(name, a)
def get_page_size(opts, for_comic=False): use_profile = opts.use_profile_size and opts.output_profile.short_name != 'default' and opts.output_profile.width <= 9999 if use_profile: w = (opts.output_profile.comic_screen_size[0] if for_comic else opts.output_profile.width) h = (opts.output_profile.comic_screen_size[1] if for_comic else opts.output_profile.height) dpi = opts.output_profile.dpi factor = 72.0 / dpi page_size = QPageSize(QSize(factor * w, factor * h), matchPolicy=QPageSize.ExactMatch) else: page_size = None if opts.custom_size is not None: width, sep, height = opts.custom_size.partition('x') if height: try: width = float(width.replace(',', '.')) height = float(height.replace(',', '.')) except: pass else: if opts.unit == 'devicepixel': factor = 72.0 / opts.output_profile.dpi else: factor = { 'point': 1.0, 'inch': inch, 'cicero': cicero, 'didot': didot, 'pica': pica, 'millimeter': mm, 'centimeter': cm }[opts.unit] page_size = QPageSize(QSize(factor * width, factor * height), matchPolicy=QPageSize.ExactMatch) if page_size is None: page_size = QPageSize( getattr(QPageSize, opts.paper_size.capitalize())) return page_size
def print_page(self, path=None): if not path: path = os.path.join(get_download_dir(), self.title()) if not path.lower().endswith('.pdf'): path += '.pdf' size = QPageSize(getattr(QPageSize, misc_config('paper_size', 'A4'))) layout = QPageLayout( size, QPageLayout.Portrait, QMarginsF(float(misc_config('margin_left', 36)), float(misc_config('margin_top', 36)), float(misc_config('margin_right', 36)), float(misc_config('margin_bottom', 36)))) self._page.printToPdf(partial(self.print_done, path), layout)
def start_print(self, data): margins = QMarginsF(0, 0, 0, 0) page_size = QPageSize(QPageSize.A4) if isinstance(data, dict): try: if 'margins' in data: margins = QMarginsF(*data['margins']) if 'size' in data: sz = data['size'] if type(getattr(QPageSize, sz, None)) is type( QPageSize.A4): # noqa page_size = QPageSize(getattr(QPageSize, sz)) else: from calibre.ebooks.pdf.image_writer import parse_pdf_page_size ps = parse_pdf_page_size(sz, data.get('unit', 'inch')) if ps is not None: page_size = ps except Exception: pass page_layout = QPageLayout(page_size, QPageLayout.Portrait, margins) self.printToPdf('rendered.pdf', page_layout) self.printing_started = True self.start_time = monotonic()
def parse_pdf_page_size(spec, unit='inch', dpi=72.0): width, sep, height = spec.lower().partition('x') if height: try: width = float(width.replace(',', '.')) height = float(height.replace(',', '.')) except Exception: pass else: if unit == 'devicepixel': factor = 72.0 / dpi else: factor = { 'point':1.0, 'inch':inch, 'cicero':cicero, 'didot':didot, 'pica':pica, 'millimeter':mm, 'centimeter':cm }.get(unit, 1.0) return QPageSize(QSizeF(factor*width, factor*height), QPageSize.Point, matchPolicy=QPageSize.ExactMatch)
def do_print(self, ok): p = QPageLayout(QPageSize(QPageSize(QPageSize.A4)), QPageLayout.Portrait, QMarginsF(72, 0, 72, 0)) self.printToPdf(self.print_finished, p)
def start_print(self): margins = QMarginsF(0, 0, 0, 0) page_layout = QPageLayout(QPageSize(QPageSize.A4), QPageLayout.Portrait, margins) self.printToPdf('rendered.pdf', page_layout) self.printing_started = True self.start_time = monotonic()