Skip to content

Commit

Permalink
Refactured the process and added more logging
Browse files Browse the repository at this point in the history
  • Loading branch information
voisardf committed Aug 28, 2024
1 parent 69e23c4 commit 3b55d99
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
3 changes: 0 additions & 3 deletions dev/config/pyramid_oereb.yml.mako
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,6 @@ pyramid_oereb:
# So be aware that fixing this value and deactivating the compute_toc_pages above may lead to wrong page numbers
# in the table of content.
default_toc_length: 2
# Depending on your toc configuration and number of disclaimer entries, the first page break of the table of content
# may appear when a real estate is concerned by one, two or more topics.
concerned_themes_for_first_toc_pagebreak: 3
# Specify any additional URL parameters that the print shall use for WMS calls
wms_url_params:
TRANSPARENT: 'true'
Expand Down
7 changes: 4 additions & 3 deletions doc/source/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ to adapt in your project configuration, database etc. when upgrading to a new ve

Version 2.6.0
-------------
* New parameter 'default_toc_length' allows to define a expected table of content pages number avoiding the extra
computation of the expected length. This value should only be set if >95% of the PDF have the same number of TOC pages.
Default setting: 1
* New parameter 'default_toc_length' allows to define a default table of content pages number avoiding a second
call for the pdf extract in most cases. This value should be set if >95% of the PDF have the same number of TOC
pages.
Default setting: 2

Version 2.5.2
-------------
Expand Down
38 changes: 33 additions & 5 deletions pyramid_oereb/contrib/print_proxy/mapfish_print/mapfish_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,8 @@ def __call__(self, value, system):
if Config.get('print', {}).get('compute_toc_pages', False):
extract_as_dict['nbTocPages'] = TocPages(extract_as_dict).getNbPages()
else:
if print_config.get('default_toc_length', 1):
if len(extract_as_dict['ConcernedTheme']) < print_config.get('concerned_themes_for_first_toc_pagebreak', 2):
extract_as_dict['nbTocPages'] = 1
else:
extract_as_dict['nbTocPages'] = print_config.get('default_toc_length', 1)
if Config.get('print', {}).get('default_toc_length', False):
extract_as_dict['nbTocPages'] = print_config.get('default_toc_length', 2)

Check warning on line 82 in pyramid_oereb/contrib/print_proxy/mapfish_print/mapfish_print.py

View check run for this annotation

Codecov / codecov/patch

pyramid_oereb/contrib/print_proxy/mapfish_print/mapfish_print.py#L81-L82

Added lines #L81 - L82 were not covered by tests
else:
extract_as_dict['nbTocPages'] = 1

Check warning on line 84 in pyramid_oereb/contrib/print_proxy/mapfish_print/mapfish_print.py

View check run for this annotation

Codecov / codecov/patch

pyramid_oereb/contrib/print_proxy/mapfish_print/mapfish_print.py#L84

Added line #L84 was not covered by tests

Expand Down Expand Up @@ -122,6 +119,8 @@ def __call__(self, value, system):
data=json.dumps(spec)
)
try:
log.debug('Validation of the TOC length with compute_toc_pages set to {} and default_toc_length set to {}'.format( \
print_config.get('compute_toc_pages'),print_config.get('default_toc_length')))
if Config.get('print', {}).get('compute_toc_pages', False):
with io.BytesIO() as pdf:
pdf.write(print_result.content)
Expand All @@ -137,14 +136,43 @@ def __call__(self, value, system):
except ValueError:
true_nb_of_toc = 1

log.debug('True number of TOC pages is {}'.format(true_nb_of_toc))
if true_nb_of_toc != extract_as_dict['nbTocPages']:
log.warning('nbTocPages in result pdf: {} are not equal to the one predicted : {}, request new pdf'.format(true_nb_of_toc,extract_as_dict['nbTocPages'])) # noqa
log.debug('Secondary PDF extract call STARTED')
extract_as_dict['nbTocPages'] = true_nb_of_toc
print_result = requests.post(

Check warning on line 144 in pyramid_oereb/contrib/print_proxy/mapfish_print/mapfish_print.py

View check run for this annotation

Codecov / codecov/patch

pyramid_oereb/contrib/print_proxy/mapfish_print/mapfish_print.py#L141-L144

Added lines #L141 - L144 were not covered by tests
pdf_url,
headers=pdf_headers,
data=json.dumps(spec)
)
log.debug('Secondary PDF extract call to fix TOC pages number FINISHED')
elif Config.get('print', {}).get('default_toc_length', 2):
with io.BytesIO() as pdf:
pdf.write(print_result.content)
pdf_reader = PdfReader(pdf)
x = []
for i in range(len(pdf_reader.outline)):
if isinstance(pdf_reader.outline[i], list):
x.append(pdf_reader.outline[i][0]['/Page']['/StructParents'])

Check warning on line 157 in pyramid_oereb/contrib/print_proxy/mapfish_print/mapfish_print.py

View check run for this annotation

Codecov / codecov/patch

pyramid_oereb/contrib/print_proxy/mapfish_print/mapfish_print.py#L149-L157

Added lines #L149 - L157 were not covered by tests
else:
x.append(pdf_reader.outline[i]['/Page']['/StructParents'])
try:
true_nb_of_toc = min(x)-1
except ValueError:
true_nb_of_toc = 1

Check warning on line 163 in pyramid_oereb/contrib/print_proxy/mapfish_print/mapfish_print.py

View check run for this annotation

Codecov / codecov/patch

pyramid_oereb/contrib/print_proxy/mapfish_print/mapfish_print.py#L159-L163

Added lines #L159 - L163 were not covered by tests

log.debug('True number of TOC pages is {}'.format(true_nb_of_toc))

Check warning on line 165 in pyramid_oereb/contrib/print_proxy/mapfish_print/mapfish_print.py

View check run for this annotation

Codecov / codecov/patch

pyramid_oereb/contrib/print_proxy/mapfish_print/mapfish_print.py#L165

Added line #L165 was not covered by tests
if true_nb_of_toc != extract_as_dict['nbTocPages']:
log.warning('nbTocPages in result pdf: {} are not equal to the one predicted : {}, request new pdf'.format(true_nb_of_toc,extract_as_dict['nbTocPages'])) # noqa
extract_as_dict['nbTocPages'] = true_nb_of_toc
log.debug('Secondary PDF extract call STARTED')

Check warning on line 169 in pyramid_oereb/contrib/print_proxy/mapfish_print/mapfish_print.py

View check run for this annotation

Codecov / codecov/patch

pyramid_oereb/contrib/print_proxy/mapfish_print/mapfish_print.py#L169

Added line #L169 was not covered by tests
print_result = requests.post(
pdf_url,
headers=pdf_headers,
data=json.dumps(spec)
)
log.debug('Secondary PDF extract call FINISHED')

Check warning on line 175 in pyramid_oereb/contrib/print_proxy/mapfish_print/mapfish_print.py

View check run for this annotation

Codecov / codecov/patch

pyramid_oereb/contrib/print_proxy/mapfish_print/mapfish_print.py#L175

Added line #L175 was not covered by tests
except PdfReadError as e:
err_msg = 'a problem occurred while generating the pdf file'
log.error(err_msg + ': ' + str(e))
Expand Down

0 comments on commit 3b55d99

Please sign in to comment.