Mise en forme PDF

Numérotation des titres (niveaux 1 et 2)
L'avant propos n'est pas numéroté (et n'apparait pas dans la table des matières)
Le titre principal du document n'est pas un niveau de titre normal
Mise en forme de la première page
This commit is contained in:
Youen 2023-05-11 02:09:29 +02:00
parent 0d60536dd5
commit 995daaeff9
5 changed files with 94 additions and 32 deletions

View file

@ -5,8 +5,9 @@ import os
source_dir = sys.argv[1]
build_dir = sys.argv[2]
insert_toc_after_page = 1
max_bookmark_level = 3
insert_toc_after_page = 2
max_bookmark_level = 2
numbered_levels = 2
index_pdf_filename = build_dir + '/weasyprint/index.pdf'
css_filename = source_dir + '/css/print-theme.css'
@ -51,7 +52,7 @@ if modified_css != original_css:
assert(subprocess.run(['weasyprint', build_dir + '/weasyprint/index.html', index_pdf_filename, '-s', css_filename]).returncode == 0)
# Insert TOC in the PDF
assert(subprocess.run(['pdftk', 'A='+index_pdf_filename, 'B='+build_dir+'/weasyprint/toc.pdf', 'cat', 'A'+str(insert_toc_after_page), 'B', 'A'+str(insert_toc_after_page+1)+'-end', 'output', build_dir + '/weasyprint/vheliotech-without-bookmarks.pdf']).returncode == 0)
assert(subprocess.run(['pdftk', 'A='+index_pdf_filename, 'B='+build_dir+'/weasyprint/toc.pdf', 'cat', 'A1-'+str(insert_toc_after_page), 'B', 'A'+str(insert_toc_after_page+1)+'-end', 'output', build_dir + '/weasyprint/vheliotech-without-bookmarks.pdf']).returncode == 0)
# Restore bookmarks
extract_bookmarks_from = index_pdf_filename
@ -70,7 +71,10 @@ with open(bookmarks_filename) as bookmarks_file:
# Remove link icon character at the end of each bookmark name (these are added by sphinx but make no sense in a PDF bookmark)
metadata = metadata.replace('', '')
# Remove bookmarks for small titles, adjust page number for remaining ones
# Remove bookmarks for small titles, adjust page number for remaining ones, and add numbering if level is not above numbered_levels
title_counters = []
for level in range(numbered_levels):
title_counters.append(0)
def filterBookmark(match):
#print('bookmark: "' + match.group(0) + '"')
level = int(match.group(2))
@ -78,7 +82,18 @@ def filterBookmark(match):
return ''
initial_page = int(match.group(3))
final_page = initial_page + toc_num_pages if initial_page > insert_toc_after_page else initial_page
return 'BookmarkBegin\nBookmarkTitle: '+match.group(1).replace(' ', ' ')+'\nBookmarkLevel: '+match.group(2)+'\nBookmarkPageNumber: '+str(final_page)+'\n'
title_counters[level - 1] = title_counters[level - 1] + 1
for l in range(level, numbered_levels):
title_counters[l] = 0
title_number = ''
if level <= numbered_levels:
for l in range(level):
title_number += str(title_counters[l]) + '.'
title_number += ' '
return 'BookmarkBegin\nBookmarkTitle: ' + title_number + match.group(1).replace('&#160;', ' ')+'\nBookmarkLevel: '+match.group(2)+'\nBookmarkPageNumber: '+str(final_page)+'\n'
metadata = re.sub('BookmarkBegin\nBookmarkTitle: (.*)\nBookmarkLevel: ([0-9]+)\nBookmarkPageNumber: ([0-9]+)\n', filterBookmark, metadata)
with open(bookmarks_filename, 'w') as bookmarks_file: