Modified pdf_unite to use PyPDF2 by default

- The user can still choose to use pdfunite from poppler-utils
This commit is contained in:
Salvador E. Tropea 2022-04-16 18:46:39 -03:00
parent b988bcb475
commit df4e60136e
3 changed files with 27 additions and 19 deletions

4
debian/control vendored
View File

@ -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.

View File

@ -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):

View File

@ -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