Reusing Board2Pdf plugin

Hello, I am trying to make my code for coloring and merging pdfs. I need it for some project. I reused part of the code from Board2Pdf plugin but only possible color is white or black. If I change color, it show only white page. PDFs are generated from KiCAD 7.0.6. I am using pymupdf library and with plugin it works but my code does not. @albin you will be probably most helpful. There is my code.

def colorize_pdf_fitz(inputFile, color):
try:
with fitz.open(inputFile) as doc:
xref_number = doc[0].get_contents()
stream_bytes = doc.xref_stream(xref_number[0])

        new_color_RG = bytes(str('125 125 0 ') + 'RG', 'ascii')
        new_color_rg = bytes(str('125 125 0 ') + 'rg', 'ascii')

        stream_bytes = re.sub(b'0.0.0.RG', new_color_RG, stream_bytes)
        stream_bytes = re.sub(b'0.0.0.rg', new_color_rg, stream_bytes)
        print(stream_bytes)
        doc.update_stream(xref_number[0], stream_bytes)
        doc.save("out.pdf", clean=True)
except:
    print("An exception occurred")
return True
![screen|690x453](upload://wNOZ1O7Lmh0Sq0MMM9UOs2nstmK.png)

Hi
I don’t have access to a computer now, but try this:

new_color = str(0.5) + ’ ’ + str(0.5) + ’ ’ + str(0.0) + ’ ’
new_color_RG = bytes(new_color + ‘RG’, ‘ascii’)
new_color_rg = bytes(new_color + ‘rg’, ‘ascii’)

Thank you so much. It was now a problem, but you pointed me the right direction. I expected range from 0 to 255, but it is 0 to 1. I just noticed your hex to rgb function. My bad, thanks. And thank you for your awesome plugin.

2 Likes

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