diff --git a/dev/config/pyramid_oereb.yml.mako b/dev/config/pyramid_oereb.yml.mako index 95a0d75825..0cc3d97a6b 100644 --- a/dev/config/pyramid_oereb.yml.mako +++ b/dev/config/pyramid_oereb.yml.mako @@ -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' diff --git a/doc/source/changes.rst b/doc/source/changes.rst index 3e6a3057d8..e82036c99d 100644 --- a/doc/source/changes.rst +++ b/doc/source/changes.rst @@ -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 ------------- diff --git a/pyramid_oereb/contrib/print_proxy/mapfish_print/mapfish_print.py b/pyramid_oereb/contrib/print_proxy/mapfish_print/mapfish_print.py index 2668372d38..b1dd94fa3d 100644 --- a/pyramid_oereb/contrib/print_proxy/mapfish_print/mapfish_print.py +++ b/pyramid_oereb/contrib/print_proxy/mapfish_print/mapfish_print.py @@ -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) else: extract_as_dict['nbTocPages'] = 1 @@ -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) @@ -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( + 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']) + else: + x.append(pdf_reader.outline[i]['/Page']['/StructParents']) + try: + true_nb_of_toc = min(x)-1 + 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 extract_as_dict['nbTocPages'] = true_nb_of_toc + log.debug('Secondary PDF extract call STARTED') print_result = requests.post( pdf_url, headers=pdf_headers, data=json.dumps(spec) ) + log.debug('Secondary PDF extract call FINISHED') except PdfReadError as e: err_msg = 'a problem occurred while generating the pdf file' log.error(err_msg + ': ' + str(e))