- fixed PDF Tool such that now it can import more types of files including PDF files made with FlatCAM

This commit is contained in:
Marius Stanciu
2020-11-17 20:08:27 +02:00
committed by Marius
parent ced0259ba4
commit de3a23e82f
5 changed files with 89 additions and 66 deletions

View File

@@ -11,6 +11,7 @@ CHANGELOG for FlatCAM beta
- the Follow Geometry outputted by the Follow Tools is now of type multigeo which means that it can be fused with other multigeo object without much pain - the Follow Geometry outputted by the Follow Tools is now of type multigeo which means that it can be fused with other multigeo object without much pain
- Punch Gerber Tool - working on the new manual pads add feature - Punch Gerber Tool - working on the new manual pads add feature
- fixed PDF Tool such that now it can import more types of files including PDF files made with FlatCAM
16.11.2020 16.11.2020

View File

@@ -532,6 +532,7 @@ class PdfParser:
else: else:
ap_list = [int(k) for k in apertures_dict.keys()] ap_list = [int(k) for k in apertures_dict.keys()]
# perhaps it's the only aperture? and in that case we need to start from 10 # perhaps it's the only aperture? and in that case we need to start from 10
if 0 in ap_list and len(ap_list) == 1:
ap_list.remove(0) ap_list.remove(0)
if not ap_list: if not ap_list:
aperture = 10 aperture = 10
@@ -874,6 +875,7 @@ class PdfParser:
else: else:
ap_list = [int(k) for k in apertures_dict.keys()] ap_list = [int(k) for k in apertures_dict.keys()]
# perhaps it's the only aperture? and in that case we need to start from 10 # perhaps it's the only aperture? and in that case we need to start from 10
if 0 in ap_list and len(ap_list) == 1:
ap_list.remove(0) ap_list.remove(0)
if not ap_list: if not ap_list:
aperture = 10 aperture = 10

View File

@@ -13,15 +13,17 @@ from shapely.geometry import Point, MultiPolygon
from shapely.ops import unary_union from shapely.ops import unary_union
from copy import deepcopy from copy import deepcopy
from io import BytesIO # from io import BytesIO
#
import zlib # import zlib
import re import re
import time import time
import logging import logging
import traceback import traceback
import os import os
from pikepdf import Pdf, parse_content_stream
import gettext import gettext
import appTranslation as fcTranslate import appTranslation as fcTranslate
import builtins import builtins
@@ -128,67 +130,83 @@ class ToolPDF(AppTool):
with self.app.proc_container.new('%s...' % _("Parsing")): with self.app.proc_container.new('%s...' % _("Parsing")):
with open(filename, "rb") as f: with open(filename, "rb") as f:
pdf = f.read() # pdf = f.read()
pdf = Pdf.open(f)
stream_nr = 0 page = pdf.pages[0]
for s in re.findall(self.stream_re, pdf): decomp_file = ''
if self.app.abort_flag: for operands, command in parse_content_stream(page):
# graceful abort requested by the user line = ''
raise grace for op in operands:
try:
stream_nr += 1 line += str(op) + ' '
log.debug("PDF STREAM: %d\n" % stream_nr) except Exception as e:
s = s.strip(b'\r\n') # print(str(e), operands, command)
pass
# https://stackoverflow.com/questions/1089662/python-inflate-and-deflate-implementations line += str(command)
# def decompress(data): decomp_file += line + '\n'
# decompressed = zlib.decompressobj( self.pdf_decompressed[short_name] = decomp_file
# -zlib.MAX_WBITS # see above
# )
# inflated = decompressed.decompress(data)
# inflated += decompressed.flush()
# return inflated
# stream_nr = 0
# for s in re.findall(self.stream_re, pdf):
# if self.app.abort_flag:
# # graceful abort requested by the user
# raise grace
#
# stream_nr += 1
# log.debug("PDF STREAM: %d\n" % stream_nr)
# s = s.strip(b'\r\n')
#
# # https://stackoverflow.com/questions/1089662/python-inflate-and-deflate-implementations
# # def decompress(data):
# # decompressed = zlib.decompressobj(
# # -zlib.MAX_WBITS # see above
# # )
# # inflated = decompressed.decompress(data)
# # inflated += decompressed.flush()
# # return inflated
#
# Convert 2 Bytes If Python 3 # Convert 2 Bytes If Python 3
def C2BIP3(string): # def C2BIP3(string):
if type(string) == bytes: # if type(string) == bytes:
return string # return string
else: # else:
return bytes([ord(x) for x in string]) # return bytes([ord(x) for x in string])
#
def inflate(data): # def inflate(data):
try: # try:
return zlib.decompress(C2BIP3(data)) # return zlib.decompress(C2BIP3(data))
except Exception: # except Exception:
if len(data) <= 10: # if len(data) <= 10:
raise # raise
oDecompress = zlib.decompressobj(-zlib.MAX_WBITS) # oDecompress = zlib.decompressobj(-zlib.MAX_WBITS)
oStringIO = BytesIO() # oStringIO = BytesIO()
count = 0 # count = 0
for byte in C2BIP3(data): # for byte in C2BIP3(data):
try: # try:
oStringIO.write(oDecompress.decompress(byte)) # oStringIO.write(oDecompress.decompress(byte))
count += 1 # count += 1
except Exception: # except Exception:
break # break
if len(data) - count <= 2: # if len(data) - count <= 2:
return oStringIO.getvalue() # return oStringIO.getvalue()
else: # else:
raise # raise
#
try: # try:
decomp = inflate(s) # decomp = inflate(s)
except Exception as e: # except Exception as e:
decomp = None # decomp = None
log.debug("ToolPDF.open_pdf() -> inflate (decompress) -> %s" % str(e)) # log.debug("ToolPDF.open_pdf() -> inflate (decompress) -> %s" % str(e))
#
try: # try:
self.pdf_decompressed[short_name] += (decomp.decode('UTF-8') + '\r\n') # self.pdf_decompressed[short_name] += (decomp.decode('UTF-8') + '\r\n')
except Exception: # except Exception:
try: # try:
self.pdf_decompressed[short_name] += (decomp.decode('latin1') + '\r\n') # self.pdf_decompressed[short_name] += (decomp.decode('latin1') + '\r\n')
except Exception as e: # except Exception as e:
log.debug("ToolPDF.open_pdf() -> decoding error -> %s" % str(e)) # log.debug("ToolPDF.open_pdf() -> decoding error -> %s" % str(e))
# self.pdf_decompressed[short_name] = decomp_file
if self.pdf_decompressed[short_name] == '': if self.pdf_decompressed[short_name] == '':
self.app.inform.emit('[ERROR_NOTCL] %s: %s' % (_("Failed to open"), str(filename))) self.app.inform.emit('[ERROR_NOTCL] %s: %s' % (_("Failed to open"), str(filename)))

View File

@@ -29,3 +29,4 @@ reportlab>=3.5
svglib svglib
gdal gdal
pyserial>=3.4 pyserial>=3.4
pikepdf>=2.0

View File

@@ -45,6 +45,7 @@ sudo -H python3 -m pip install --upgrade \
pyqt5 \ pyqt5 \
reportlab \ reportlab \
svglib \ svglib \
pyserial pyserial \
pikepdf
sudo -H easy_install -U distribute sudo -H easy_install -U distribute