From df4e60136e8b26e9006d72e776e296fdea17e583 Mon Sep 17 00:00:00 2001 From: "Salvador E. Tropea" Date: Sat, 16 Apr 2022 18:46:39 -0300 Subject: [PATCH] Modified pdf_unite to use PyPDF2 by default - The user can still choose to use pdfunite from poppler-utils --- debian/control | 4 ++-- kibot/out_pcb_print.py | 9 ++++----- kibot/out_pdfunite.py | 33 +++++++++++++++++++++------------ 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/debian/control b/debian/control index 9f2dfe33..8bc8c334 100644 --- a/debian/control +++ b/debian/control @@ -11,8 +11,8 @@ Package: kibot Architecture: all Multi-Arch: foreign Depends: ${misc:Depends}, ${python3:Depends}, python3-distutils, python3-yaml, kicad (>= 5.1.6), python3-wxgtk4.0 -Recommends: kibom.inti-cmnb (>= 1.8.0), interactivehtmlbom.inti-cmnb, pcbdraw, imagemagick, librsvg2-bin, python3-xlsxwriter, rar, poppler-utils, python3-lxml -Suggests: pandoc, texlive-latex-base, texlive-latex-recommended, git, ghostscript +Recommends: kibom.inti-cmnb (>= 1.8.0), interactivehtmlbom.inti-cmnb, pcbdraw, imagemagick, librsvg2-bin, python3-xlsxwriter, rar, python3-lxml +Suggests: pandoc, texlive-latex-base, texlive-latex-recommended, git, ghostscript, poppler-utils Description: KiCad Bot KiBot is a program which helps you to automate the generation of KiCad output documents easily, repeatable, and most of all, scriptably. diff --git a/kibot/out_pcb_print.py b/kibot/out_pcb_print.py index a05b6f7e..3c379436 100644 --- a/kibot/out_pcb_print.py +++ b/kibot/out_pcb_print.py @@ -36,7 +36,6 @@ VIATYPE_THROUGH = 3 VIATYPE_BLIND_BURIED = 2 VIATYPE_MICROVIA = 1 -# - Use PyPDF2 for pdfunite # - Analyze KiCad 6 long delay @@ -109,14 +108,14 @@ def to_inches(w): return val -def create_pdf_from_pages(input_folder, input_files, output_fn): +def create_pdf_from_pages(input_files, output_fn): output = PyPDF2.PdfFileWriter() # Collect all pages open_files = [] er = None for filename in input_files: try: - file = open(os.path.join(input_folder, filename), 'rb') + file = open(filename, 'rb') open_files.append(file) pdf_reader = PyPDF2.PdfFileReader(file) page_obj = pdf_reader.getPage(0) @@ -172,8 +171,8 @@ def create_pdf_from_svg_pages(input_folder, input_files, output_fn): for svg_file in input_files: pdf_file = svg_file.replace('.svg', '.pdf') svg_to_pdf(input_folder, svg_file, pdf_file) - svg_files.append(pdf_file) - create_pdf_from_pages(input_folder, svg_files, output_fn) + svg_files.append(os.path.join(input_folder, pdf_file)) + create_pdf_from_pages(svg_files, output_fn) class LayerOptions(Layer): diff --git a/kibot/out_pdfunite.py b/kibot/out_pdfunite.py index 682e6d8f..6826254a 100644 --- a/kibot/out_pdfunite.py +++ b/kibot/out_pdfunite.py @@ -15,6 +15,7 @@ from .misc import MISSING_TOOL, WRONG_INSTALL, WRONG_ARGUMENTS, INTERNAL_ERROR, from .optionable import Optionable, BaseOptions from .registrable import RegOutput from .macros import macros, document, output_class # noqa: F401 +from .out_pcb_print import create_pdf_from_pages from . import log logger = log.get_logger() @@ -44,6 +45,8 @@ class PDFUniteOptions(BaseOptions): """ Name for the generated PDF (%i=name of the output %x=pdf) """ self.outputs = FilesList """ [list(dict)] Which files will be included """ + self.use_external_command = False + """ Use the `pdfunite` tool instead of PyPDF2 Python module """ super().__init__() self._expand_ext = 'pdf' @@ -101,6 +104,20 @@ class PDFUniteOptions(BaseOptions): files = self.get_files(output, no_out_run=True) return files + def run_external(self, files, output): + cmd = ['pdfunite']+files+[output] + logger.debug('Running: {}'.format(cmd)) + try: + check_output(cmd, stderr=STDOUT) + except FileNotFoundError: + logger.error('Missing `pdfunite` command, install it (poppler-utils)') + exit(MISSING_TOOL) + except CalledProcessError as e: + logger.error('Failed to invoke pdfunite command, error {}'.format(e.returncode)) + if e.output: + logger.error('Output from command: '+e.output.decode()) + exit(WRONG_INSTALL) + def run(self, output): # Output file name logger.debug('Collecting files') @@ -114,18 +131,10 @@ class PDFUniteOptions(BaseOptions): logger.debug('Generating `{}` PDF'.format(output)) if os.path.isfile(output): os.remove(output) - cmd = ['pdfunite']+files+[output] - logger.debug('Running: {}'.format(cmd)) - try: - check_output(cmd, stderr=STDOUT) - except FileNotFoundError: - logger.error('Missing `pdfunite` command, install it (poppler-utils)') - exit(MISSING_TOOL) - except CalledProcessError as e: - logger.error('Failed to invoke pdfunite command, error {}'.format(e.returncode)) - if e.output: - logger.error('Output from command: '+e.output.decode()) - exit(WRONG_INSTALL) + if self.use_external_command: + self.run_external(files, output) + else: + create_pdf_from_pages(files, output) @output_class