Board2Pdf v1.1 released in PCM

You have a typo in package name. But like albin said, it’s not needed.

I see forgot an s.
But after installing fitz I still get this error:

PyMuPdf wasn’t loaded.

Run ‘python -m pip install --upgrade pdfCropMargins’ from the KiCad Command Prompt to install pdfCropMargins which includes PyMuPdf. Then restart the PCB Editor.

sudo didn’t work as expected … as AB007 wrote.
As i understood Board2Pdf plugin has changed, so i had uninstalled Board2Pdf and again installed with PCM an started PCBNEW:
grafik

my situation:

That’s a new one. Which version of fitz do you have?

Successfully installed fitz-0.0.1.dev2

Long version:
Collecting fitz
Using cached fitz-0.0.1.dev2-py2.py3-none-any.whl (20 kB)
Requirement already satisfied: scipy in ./.local/lib/python3.10/site-packages (from fitz) (1.10.1)
Requirement already satisfied: nibabel in ./.local/lib/python3.10/site-packages (from fitz) (5.0.1)
Requirement already satisfied: pandas in ./.local/lib/python3.10/site-packages (from fitz) (1.5.3)
Requirement already satisfied: nipype in ./.local/lib/python3.10/site-packages (from fitz) (1.8.5)
Requirement already satisfied: httplib2 in /usr/lib/python3/dist-packages (from fitz) (0.20.2)
Requirement already satisfied: configparser in ./.local/lib/python3.10/site-packages (from fitz) (5.3.0)
Requirement already satisfied: pyxnat in ./.local/lib/python3.10/site-packages (from fitz) (1.5)
Requirement already satisfied: configobj in /usr/lib/python3/dist-packages (from fitz) (5.0.6)
Requirement already satisfied: numpy in /usr/lib/python3/dist-packages (from fitz) (1.21.5)
Requirement already satisfied: pyparsing!=3.0.0,!=3.0.1,!=3.0.2,!=3.0.3,<4,>=2.4.2 in /usr/lib/python3/dist-packages (from httplib2->fitz) (2.4.7)
Requirement already satisfied: setuptools in /usr/lib/python3/dist-packages (from nibabel->fitz) (59.6.0)
Requirement already satisfied: packaging>=17 in /usr/lib/python3/dist-packages (from nibabel->fitz) (21.3)
Requirement already satisfied: rdflib>=5.0.0 in ./.local/lib/python3.10/site-packages (from nipype->fitz) (6.2.0)
Requirement already satisfied: traits!=5.0,<6.4,>=4.6 in ./.local/lib/python3.10/site-packages (from nipype->fitz) (6.3.2)
Requirement already satisfied: prov>=1.5.2 in ./.local/lib/python3.10/site-packages (from nipype->fitz) (2.0.0)
Requirement already satisfied: pydot>=1.2.3 in ./.local/lib/python3.10/site-packages (from nipype->fitz) (1.4.2)
Requirement already satisfied: looseversion in ./.local/lib/python3.10/site-packages (from nipype->fitz) (1.1.2)
Requirement already satisfied: etelemetry>=0.2.0 in ./.local/lib/python3.10/site-packages (from nipype->fitz) (0.3.0)
Requirement already satisfied: simplejson>=3.8.0 in /usr/lib/python3/dist-packages (from nipype->fitz) (3.17.6)
Requirement already satisfied: click>=6.6.0 in /usr/lib/python3/dist-packages (from nipype->fitz) (8.0.3)
Requirement already satisfied: networkx>=2.0 in ./.local/lib/python3.10/site-packages (from nipype->fitz) (3.0)
Requirement already satisfied: filelock>=3.0.0 in /usr/lib/python3/dist-packages (from nipype->fitz) (3.6.0)
Requirement already satisfied: python-dateutil>=2.2 in ./.local/lib/python3.10/site-packages (from nipype->fitz) (2.8.2)
Requirement already satisfied: pytz>=2020.1 in /usr/lib/python3/dist-packages (from pandas->fitz) (2022.1)
Requirement already satisfied: future>=0.16 in ./.local/lib/python3.10/site-packages (from pyxnat->fitz) (0.18.3)
Requirement already satisfied: requests>=2.20 in /usr/lib/python3/dist-packages (from pyxnat->fitz) (2.25.1)
Requirement already satisfied: six>=1.15 in /usr/lib/python3/dist-packages (from pyxnat->fitz) (1.16.0)
Requirement already satisfied: lxml>=4.3 in ./.local/lib/python3.10/site-packages (from pyxnat->fitz) (4.9.2)
Requirement already satisfied: pathlib>=1.0 in ./.local/lib/python3.10/site-packages (from pyxnat->fitz) (1.0.1)
Requirement already satisfied: ci-info>=0.2 in ./.local/lib/python3.10/site-packages (from etelemetry>=0.2.0->nipype->fitz) (0.3.0)
Requirement already satisfied: isodate in ./.local/lib/python3.10/site-packages (from rdflib>=5.0.0->nipype->fitz) (0.6.1)
Installing collected packages: fitz
Successfully installed fitz-0.0.1.dev2

python3-fitz è già alla versione più recente (1.16.11-1)

That seems like very old versions…

I’ve got:


  • KiCad 7.0 Command Prompt

You may now invoke python or pip targeting kicad’s install

C:\Users\denne\Documents\KiCad\7.0>python -m pip list
Package Version



pdfCropMargins 1.1.18

PyMuPDF 1.21.1
PyPDF2 2.12.1

I’ve added the ability for the user to choose between PyMuPdf (fitz) and pypdf (currently PyPDF4) in the GUI. The code is found here

image

The selected library is currently used for coloring. PyPDF4 is always used for merging and adding all the pages to a final file.

The problem is that I’m stuck with a problem that I don’t understand.

All works fine as long as there’s no parentheses in any footprint description. This has to do with Issue #14302 that got fixed today, but I’m still using 7.0.1 because I’d like to find a workaround.

If there are parentheses in at least one footprint description, PyPDF4 is supposed to throw a warning that I just want to ignore. The problem is that I’m getting an exception instead.

What’s mind-boggling is that I only get the exception if I don’t open the Python scripting console before running the plugin!

Can someone help me understand why it matters if I open the Python scripting console or not before running the plugin? It doesn’t matter if I load the plugin from the scripting console or if I press the button in the toolbar. Perhaps @qu1ck knows something? You seem to know most things there is to know about Python in KiCad.

Steps to reproduce a working scenario:

  1. Open PCB Editor.
  2. Open complex_hierarchy.kicad_pcb or some other board with parentheses in at least one footprint description.
  3. Open the Python scripting console.
  4. Open Board2Pdf using the icon in the toolbar.
  5. Press Plot.

This works just fine. It doesn’t matter if the Python scripting console is left opened or closed before Board2Pdf is opened. The only thing that’s important is that it is opened.

Sometimes (not always) I get a windows that lists all warnings, but it always works.

Steps to reproduce a non-working scenario:

  1. Open PCB Editor.
  2. Open complex_hierarchy.kicad_pcb or some other board with parentheses in at least one footprint description.
  3. Do NOT Open the Python scripting console.
  4. Open Board2Pdf using the icon in the toolbar.
  5. Press Plot.

Doing this always throws an AttributeError:

image


I cannot figure out why it matters if I open the Python scripting console or not. And I cannot figure out a way to solve this. It's not just that I get an AttributeError. The file that is supposed to be created is left empty. Even if I completely remove my error handling in colorize_pdf_pypdf(), the file that is created is still left empty.

Hi Albin …
for me it’s not so clear which fitz is meant …
sudo apt install python3-fitz says:
python3-fitz Version (1.19.2+ds1-1ubuntu1)
Now pip3 install pdfCropMargins is running:
Defaulting to user installation because normal site-packages is not writeable
Collecting pdfCropMargins
Downloading pdfCropMargins-1.1.18-py2.py3-none-any.whl (1.8 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.8/1.8 MB 6.6 MB/s eta 0:00:00
Requirement already satisfied: wheel in /usr/lib/python3/dist-packages (from pdfCropMargins) (0.37.1)
Requirement already satisfied: PyMuPDF>=1.19.6 in ./.local/lib/python3.10/site-packages (from pdfCropMargins) (1.21.1)
Requirement already satisfied: PySimpleGUI>=4.19.0.6 in ./.local/lib/python3.10/site-packages (from pdfCropMargins) (4.60.4)
Requirement already satisfied: pillow>=9.3.0 in ./.local/lib/python3.10/site-packages (from pdfCropMargins) (9.4.0)
Requirement already satisfied: PyPDF2<3.0.0,>=2.11.0 in ./.local/lib/python3.10/site-packages (from pdfCropMargins) (2.12.1)
Installing collected packages: pdfCropMargins
Successfully installed pdfCropMargins-1.1.18

python3 -m pip list
PyMuPDF 1.21.1
PyPDF2 2.12.1

I was manually copying plugin into /.local/share/kicad/7.0/3rdparty/com_gitlab_dennevi_Board2Pdf/ after i have installed installed V1.1 the plugin runs with selected pypdf(slow) option. Thats great! Something new to play with !!!

Many thanks again for all of your awesome support !!!

That happens because kicad scripting console remaps stderr when launched.
It’s technically a bug in a lib to assume that stdout/stderr always exist. But it will only show itself in a very specific circumstances like when python program is running inside embedded interpreter that is a windows UI app and therefore doesn’t have a console io streams at all.

You can verify this with this test plugin:

import pcbnew

class TT(pcbnew.ActionPlugin):
    def defaults(self):
        super().defaults()
        self.name = "stdout test"

    def Run(self):
        import sys
        import wx
        wx.MessageBox(f"stdout: {sys.stdout}\nstderr: {sys.stderr}")

TT().register()

If you launch it before opening scripting console you will see both output streams are None. After scripting console is launched, stderr is a valid stream.

Looking at PyPDF4 code, you can pass custom file-like object to PdfFileReader as warndest parameter, these warnings will end up here. If you pass nothing it will default to stderr, which gives you the exception above.
I suggest in your plugin code check if sys.stderr is None and if it is then pass custom warndest which either voids the output or you can use wx.PyOnDemandOutputWindow. Same as the scripting console does: scripting/kipython_frame.cpp · fc6279a2eaf63ff55fcdf8f110a07e0e41be71ae · KiCad / KiCad Source Code / kicad · GitLab

1 Like

Thanks a lot for a great explanation! You really go above and beyond what I could even dare to ask for. That sounds like a great idea and I’ll start looking at this right away.

1 Like

Hi @albin , these are the versions of the packages :
python3 -m pip list
pdfCropMargins 1.1.18
PyMuPDF 1.21.1
PyPDF2 2.12.1

However I tried your last modification and by selecting the pypdf library the plugin works correctly and I can generate pdf files.

@ciondo61 PyMuPDF 1.21.1 should work fine as long as none of the footprint descriptions contains parentheses. It should also work with KiCad 7.0.0 and the latest testing build. Happy to hear that the pypdf option works for you!

I’ve now pushed a new version that handles warnings caused by this issue. It’s now possible to choose between PyMuPdf and PyPDF4 for coloring and merging independently.

If you’re not able to install PyMuPDF (fitz) on your system, please use PyPDF4 which is already included.

image

PyPDF4 is able to color the pdfs created by KiCad 7.0.1, even if footprint descriptions contains parentheses. So if you’re using KiCad 7.0.1 and PyMuPdf isn’t working for you, please select PyPDF4 instead.

If you are able to install PyMuPDF 1.21.1, and you’re not using 7.0.1 or your footprint descriptions doesn’t contain any parentheses, then I really recommend using the PyMuPdf alternative. It’s way faster when plotting larger boards!

@AB007, @tTom, @ciondo61, @canemichael97 and @luppa could you please test the latest committed version? Anyone else also of course! But these are the users that I know have been having problems.

It’s not yet available in the PCM, I’d like it to be tested first. Instructions on how to install manually are found here.

Yes it’s working now (the slow option) with no error. The fitz version did not work.
I also have a suggestion: the plugin is nice but you should consider the option to arrange all views in the same sheet as long as it is space (or to choose number of pages/ views/ pages or scaling); under each view can be placed a view label (Top view, Bottom view, etc…).

Anyway very good job! looks very nice on my MAC.

Best Regards,
Andrei

Hi albin,
just installed the new commit, but i can’t choose. The PY radio buttons are all highlighted :

@AB007 Thanks for testing! Glad it worked.

If you have fitz installed, it should work with the latest testing build of KiCad. I don’t think there are any testing builds available for MacOS though. For Windows, the testing builds are found here:
https://downloads.kicad.org/kicad/windows/explore/7.0-testing

@tTom Thanks for testing! I’m glad that you found this and that I didn’t push this version to the PCM yesterday. :smiley: Not sure why it behaves differently on Windows and Linux, but my implementation of the radio buttons where not that great.

I’ve pushed a new commit where this is hopefully solved. I see no difference in Windows. Please test with the latest commit.

Hi @albin , I confirm that the slow version (pypdf4) works correctly, while the fitz version does not work. However pypdf4 is slow but still usable.

Best Regards,
Stefano

1 Like

Hi @albin ,
its my pleasure, that i’m able to do something here with my small knowledge. I’m still proud of that i can put your commits to the right place for troubleshooting :smile:
Now the radio buttons behave as expected, and pdf files were generated (pypdf4), wonderful !!!