Schematics plotting to PDF and Javascript

Hello, apparently KiCad v7 does embed Javascript stuff in PDF files when when plot schematics to PDF.

I understand it’s supposed to add features to the PDFs so that each part can be inspected, but it does make the PDFs much larger, it just doesn’t add any value for viewers which don’t provide Javascript support (or which have it disabled), and it may potentially cause security issues?

Not saying that KiCad should get rid of it altogether, but at least make it optional. I haven’t found any plot option which could stop embedding Javascript in the generated PDFs. Did I miss it? Is there a workaround?

You can print to pdf instead of plotting.

1 Like

To be honest, I absolutely love the smart pdf functions. When you work in more professional environments this gives the opportunity to store more information (like internal or external part numbers, additional ratings, etc.) in the schematic without overloading the actual schematic apperance.

What I still miss is a pdf containing ALL project information (schematic, layout, stackup information, BOM, etc.) in ONE smart pdf. I work in EMC engineering at a big automotive OEM and we have a lot of internal test equipment, communication partners, self-developed amplifiers and more that is used by many engineers who only need to read information without installing specialized software like Kicad.

What I also noticed - as you already mentioned - is the extreme size of the generated files. Today I had to send a smart schematic via mail that had more than 25Mb for only 9 pages. Maybe there is a more efficient way to compress this. The actual kicad files only have like 800 kB all together.

Yeah but what if we want you to be dishonest? :laughing:

I am ignorant about Javascript but if you plot to pdf, a modern pdf reader can “find” a reference designation such as R27. If that schematic needs to be read by someone who has a .pdf reader but not KiCad, the ability to find reference designations is a big help. As @RacingJoe said, you can alternatively print to .pdf and then you lose that…

It’s optional in 7.99. (Or, at least it will be in tomorrow’s build. :wink: )

5 Likes

Thanks Jeff. That also shows that I’m probably far from being the only one asking. Hope this will stick to the v8 release, but even so, that’s still a few months from now. I can’t use dev builds for work.

The reasons I’m asking to make it optional were stated in my OP, I don’t think I need to go over them again.

Just note that regarding the “security” factor, this is a serious concern. Especially for those working in professional environments. The IT dept of many companies just reject PDF files with “active content” (as it can potentially contain harmful stuff), so that’s certainly something one should always make optional IMO. For those that find this useful, and for the use cases for which this is useful and can be used, fine.

“Printing to PDF” is the usual workaround suggested in general (not specifically KiCad) for stripping out active content from a PDF. Unfortunately, it does strip more than just that. We also lose the index (table of contents that shows the sheet names), which is handy for larger schematics, and which per se doesn’t require active content, it’s a basic PDF feature. Also, printing to PDF is less convenient in KiCad than plotting, it requires more clicks and going through more dialogs, it doesn’t retain the path we want to save the plots to (while KiCad plot does), etc. Not quite equivalent, unfortunately.

I’ve looked at kicad-cli, just in case it provided the option that we don’t get in the GUI, but it doesn’t - same options. (So btw, to Jeff: I’ve taken a quick look at your implementation in gitlab, pretty cool, but I’m not sure you have handled the option via kicad-cli as well. Maybe you have, if not, that’s something worth adding too. For those who automate things.)

For the intriguing factor, I tried using ghostscript to strip off the active content from the PDF, but for some reason, it just won’t, whatever the options I tried (even forcing PDF/A, which should normally never output active content as it’s non-compliant with PDF/A.) That’s probably something to be submitted to the ghostscript devs. What I can just say is that ghostscript detects errors in the schematic PDFs generated by KiCad (which may explain the above issue, not sure):

The following errors were encountered at least once while processing this file:
        circular reference to indirect object

The following warnings were encountered at least once while processing this file:
        A problem was encountered trying to preserve the Outlines

   **** This file had errors that were repaired or ignored.
   **** The file was produced by: 
   **** >>>> KiCad PDF <<<<
   **** Please notify the author of the software that produced this
   **** file that it does not conform to Adobe's published PDF
   **** specification.

As a workaround you could use ‘pyPDF2’:

I took a swing at the CLI (Add --exclude-pdf-property-popups to schematic plotting CLI. (3e4d5d77) · Commits · KiCad / KiCad Source Code / kicad · GitLab), but I don’t use it so you guys are on your own for testing…

[Note: both fixes are in master of 7.99, so they will automatically be in 8.0.]

3 Likes

Thanks for the tip about PyPDF2, I’ll give it a look. Not sure it will do a better job than ghostscript or qpdf that I’ve tried though. I’ll see.

For ghostscript, what works is to convert the PDF to postscript and then postscript to PDF, but then you lose the index as well, which I would prefer to keep. Also, it seems not to yield exactly the same rendering quality, although it’s just an impression on a few tests here and that may be a bit subjective.

Thanks Jeff. I can’t test kicad-cli right now unfortunately, as there is a bug with the latest wxWidgets that makes it crash (at least on Linux). A fix should be coming with the next wxWidgets release, but one fix from the KiCad side would be to build kicad-cli as a true console app.

The issue has been raised a couple days ago on the wxWidgets project (not by me).

Regarding PyPDF2, it’s now become just pypdf: Welcome to pypdf — pypdf 3.16.4 documentation

Turns out very flexible, and it did exactly what I wanted. So, that’ll be a solution until v8, and can prove handy for a number of other purposes. You can also easily add/remove metadata, and a lot of other things.

The Python function I wrote is the following, feel free to use it to write your own scripts:

from pypdf import PdfReader, PdfWriter

def PDF_Copy_RemoveAnnotations(SrcPDF_FilePath, DestPDF_FilePath, Annotations):
	# Open PDF File.
	SrcPDF = PdfReader(SrcPDF_FilePath)
	
	# Create a clone of the document, only removing the given type(s) of annotations.
	DestPDF = PdfWriter()
	DestPDF.clone_reader_document_root(SrcPDF)
	DestPDF.remove_annotations(Annotations)
	
	with open(DestPDF_FilePath, 'wb') as fp:
		DestPDF.write(fp)

The only annotation KiCad v7 apparently uses is ‘/Link’, so you can call this function with:

PDF_Copy_RemoveAnnotations(SrcPDF_FilePath, DestPDF_FilePath, '/Link')

Alternatively, you can also make it remove all annotations without filtering any particular one.
With this, it does retain the index (aka. bookmarks, now called “outline items”) and removes all annotations with Javascript content.

1 Like

it could be usefull to have the cli option also for pcb files… (if not already available)

Already available there.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.