def concat_pdfs_gs(destination, *pdfs): """Concatenate all the named PDFs.""" cmd = ['gs'] cmd += ['-dBATCH', '-dNOPAUSE', '-sDEVICE=pdfwrite'] cmd += ['-sOutputFile=%s' % destination, '-f'] cmd.extend(x for x in pdfs if x is not None) run(cmd)
def reshape_pdf(self, pdf, dir=config.DEFAULT_DIR, centre_start=False, centre_end=False, even_pages=True): """Spin the pdf for RTL text, resize it to the right size, and shift the gutter left and right""" ops = [] if self.gutter: ops.append('shift') if even_pages: ops.append('even_pages') gutter = self.gutter if dir == 'RTL': gutter = -gutter if not ops: return os.putenv("OBJAVI_SCRIPT_DIR", config.SCRIPT_DIR) cmd = ['pdfedit', '-s', '%s/wk_objavi.qs' % config.SCRIPT_DIR, 'dir=%s' % dir, 'filename=%s' % pdf, 'output_filename=%s' % pdf, 'operation=%s' % ','.join(ops), 'width=%s' % self.width, 'height=%s' % self.height, 'offset=%s' % gutter, 'centre_start=%s' % centre_start, 'centre_end=%s' % centre_end, ] run(cmd)
def reshape_pdf(self, pdf, dir=config.DEFAULT_DIR, centre_start=False, centre_end=False, even_pages=True): """Spin the pdf for RTL text, resize it to the right size, and shift the gutter left and right""" ops = [] if self.gutter: ops.append('shift') if even_pages: ops.append('even_pages') gutter = self.gutter if dir == 'RTL': gutter = -gutter if not ops: return cmd = ['pdfedit', '-s', 'wk_objavi.qs', 'dir=%s' % dir, 'filename=%s' % pdf, 'output_filename=%s' % pdf, 'operation=%s' % ','.join(ops), 'width=%s' % self.width, 'height=%s' % self.height, 'offset=%s' % gutter, 'centre_start=%s' % centre_start, 'centre_end=%s' % centre_end, ] run(cmd)
def render(html_path, pdf_path, **kwargs): """Creates a book PDF from the provided HTML document. """ program = "renderer" params = [ "-platform", "xcb", "-output", pdf_path, ] if kwargs.has_key("page_config"): params += ["-page-config", kwargs.get("page_config")] custom_css_file = None custom_css_text = kwargs.get("custom_css") if custom_css_text: custom_css_file = tempfile.NamedTemporaryFile(prefix="renderer-", suffix=".css", delete=True) custom_css_file.write(custom_css_text) custom_css_file.flush() params += ["-custom-css", custom_css_file.name] cmd = [ program ] + params + [ html_path ] try: book_utils.run(cmd) finally: if custom_css_file: custom_css_file.close()
def rotate_pdf(pdfin, pdfout): """Turn the PDF on its head""" cmd = ['pdftk', pdfin, 'cat', '1-endD', 'output', pdfout ] run(cmd)
def resize_pdf(pdf, width, height): ops = ["resize"] cmd = ["pdfedit", "-s", "wk_objavi.qs", 'filename=%s' % pdf, 'output_filename=%s' % pdf, 'operation=%s' % ','.join(ops), 'width=%s' % width, 'height=%s' % height, ] run(cmd)
def resize_pdf(pdf, width, height): ops = ["resize"] cmd = ["pdfedit", "-s", "scripts/wk_objavi.qs", 'filename=%s' % pdf, 'output_filename=%s' % pdf, 'operation=%s' % ','.join(ops), 'width=%s' % width, 'height=%s' % height, ] run(cmd)
def make_cover_pdf(self, html_path, pdf_path): width = self.width * POINT_2_MM height = self.height * POINT_2_MM args = [ "-T", "0", "-B", "0", "-L", "0", "-R", "0", "--page-width", repr(width), "--page-height", repr(height), ] cmd = [config.WKHTMLTOPDF] + args + [html_path, pdf_path] run(cmd)
def make_cover_pdf(self, pdf, spine_width): width = self.width * POINT_2_MM height = self.height * POINT_2_MM spine_width = spine_width * POINT_2_MM # XXX for now makes a blank cover cmd = [config.WKHTMLTOPDF, "-q", "--page-width", str(2 * width + spine_width), "--page-height", str(height), "--load-error-handling", "ignore", "/dev/zero", pdf ] run(cmd)
def embed_all_fonts(pdf_file): tmp_file = pdf_file + '.pre-embed.pdf' os.rename(pdf_file, tmp_file) cmd = ['gs', '-q', # no messages to stdout '-dSAFER', # read-only '-dNOPLATFONTS', # no platform fonts '-dNOPAUSE', # don't prompt user '-dBATCH', # ? undocumented '-sDEVICE=pdfwrite', #'-sPAPERSIZE=letter', '-dCompatibilityLevel=1.4', # pdf v4 '-dPDFSETTINGS=/printer', # "printer optimised" pdf '-dMaxSubsetPct=100', # Always use a subset font '-dSubsetFonts=false', # never use a subset font (overrides above) '-dEmbedAllFonts=true', # As it says #'-sOutputFile=-', # write to stdout '-sOutputFile=%s' %(pdf_file), # write to file #could also use -dPDFX to make PDF/X3 #'-f', # ? '--', ] + [tmp_file] run(cmd)
def make_raw_pdf(self, html, pdf, outline=False, outline_file=None, page_num=None): if self.columns == 1: html_url = path2url(html, full=True) func = getattr(self, '_%s_command' % self.engine) cmd = func(html_url, pdf, outline=outline, outline_file=outline_file, page_num=page_num) run(cmd) else: #For multiple columns, generate a narrower single column pdf, and #paste it into columns using pdfnup. printable_width = self.width - 2.0 * self.side_margin - self.gutter column_width = (printable_width - (self.columns - 1) * self.column_margin) / self.columns page_width = column_width + self.column_margin side_margin = self.column_margin * 0.5 if 'PDFGEN' in config.DEBUG_MODES: log("making columns with:") for k, v in locals().iteritems(): log("%s: %r" % (k, v)) for k in ('width', 'side_margin', 'gutter', 'column_margin', 'columns', 'height'): log("self.%s: %r" % (k, getattr(self, k))) columnmaker = PageSettings(self.tmpdir, (page_width, self.height), gutter=0, top_margin=self.top_margin, side_margin=side_margin, bottom_margin=self.bottom_margin, grey_scale=self.grey_scale, engine=self.engine ) column_pdf = pdf[:-4] + '-single-column.pdf' columnmaker.make_raw_pdf(html, column_pdf, outline=outline, outline_file=outline_file, page_num=None) columnmaker.reshape_pdf(column_pdf) # pdfnup seems to round down to an even number of output # pages. For example, if a book fills 13 pages, it will # clip it to 12. So it is necessary to add blank pages to # round it up to an even number of output pages, which is # to say a multiple of (self.columns * 2) input pages. column_pages = count_pdf_pages(column_pdf) overflow_pages = column_pages % (self.columns * 2) if overflow_pages: extra_pages = self.columns * 2 - overflow_pages else: extra_pages = 0 cmd = [PDFNUP, '--nup', '%sx1' % int(self.columns), #'--paper', papersize.lower() + 'paper', '--outfile', pdf, '--noautoscale', 'true', '--orient', 'portrait', '--paperwidth', '%smm' % int(self.width * POINT_2_MM), '--paperheight', '%smm' % int(self.height * POINT_2_MM), #'--tidy', 'false', '--pages', '1-last%s' % (',{}' * extra_pages,), #'--columnstrict', 'true', #'--column', 'true', column_pdf ] run(cmd)
def concat_pdfs(destination, *pdfs): """Join all the named pdfs together into one and save it as <name>""" cmd = ['pdftk'] cmd.extend(x for x in pdfs if x is not None) cmd += ['cat', 'output', destination] run(cmd)
def make_raw_pdf(self, html, pdf, outline=False, outline_file=None, page_num=None): if self.columns == 1: html_url = path2url(html) func = getattr(self, '_%s_command' % self.engine) cmd = func(html_url, pdf, outline=outline, outline_file=outline_file, page_num=page_num) run(cmd) else: #For multiple columns, generate a narrower single column pdf, and #paste it into columns using pdfnup. printable_width = self.width - 2.0 * self.side_margin - self.gutter column_width = (printable_width - (self.columns - 1) * self.column_margin) / self.columns page_width = column_width + self.column_margin side_margin = self.column_margin * 0.5 if 'PDFGEN' in config.DEBUG_MODES: log("making columns with:") for k, v in locals().iteritems(): log("%s: %r" % (k, v)) for k in ('width', 'side_margin', 'gutter', 'column_margin', 'columns', 'height'): log("self.%s: %r" % (k, getattr(self, k))) columnmaker = PageSettings(self.workdir, (page_width, self.height), gutter=0, top_margin=self.top_margin, side_margin=side_margin, bottom_margin=self.bottom_margin, grey_scale=self.grey_scale, engine=self.engine ) column_pdf = pdf[:-4] + '-single-column.pdf' columnmaker.make_raw_pdf(html, column_pdf, outline=outline, outline_file=outline_file, page_num=None) columnmaker.reshape_pdf(column_pdf) # pdfnup seems to round down to an even number of output # pages. For example, if a book fills 13 pages, it will # clip it to 12. So it is necessary to add blank pages to # round it up to an even number of output pages, which is # to say a multiple of (self.columns * 2) input pages. column_pages = count_pdf_pages(column_pdf) overflow_pages = column_pages % (self.columns * 2) if overflow_pages: extra_pages = self.columns * 2 - overflow_pages else: extra_pages = 0 cmd = [config.PDFNUP, '--nup', '%sx1' % int(self.columns), #'--paper', papersize.lower() + 'paper', '--outfile', pdf, '--noautoscale', 'true', '--orient', 'portrait', '--paperwidth', '%smm' % int(self.width * POINT_2_MM), '--paperheight', '%smm' % int(self.height * POINT_2_MM), #'--tidy', 'false', '--pages', '1-last%s' % (',{}' * extra_pages,), #'--columnstrict', 'true', #'--column', 'true', column_pdf ] run(cmd)