Browse Source
- Mise à jour automatique du nombre de pages dans le CSS - Intégration des bookmarks dans le PDF finalmain
Youen
2 years ago
3 changed files with 60 additions and 2 deletions
@ -0,0 +1,24 @@ |
|||||||
|
import subprocess |
||||||
|
import re |
||||||
|
|
||||||
|
pdf_filename = 'build/weasyprint/index.pdf' |
||||||
|
css_filename = 'source/css/print-theme.css' |
||||||
|
additional_pages = 2 |
||||||
|
|
||||||
|
# count pages in index.pdf |
||||||
|
pdfinfo = subprocess.run(['pdfinfo', pdf_filename], stdout=subprocess.PIPE) |
||||||
|
pages_match = re.search('\\nPages:\s+([0-9]+)\\n', pdfinfo.stdout.decode()) |
||||||
|
num_pages = int(pages_match.group(1)) |
||||||
|
print('index.pdf: ' + str(num_pages) + ' pages') |
||||||
|
|
||||||
|
num_pages = num_pages + additional_pages # account for table of content that will be added later |
||||||
|
|
||||||
|
# update the CSS file with the correct number of pages |
||||||
|
|
||||||
|
with open(css_filename) as css_file: |
||||||
|
css = css_file.read() |
||||||
|
|
||||||
|
css = re.sub('content: counter\(page\) "/[0-9]+";', 'content: counter(page) "/'+str(num_pages)+'";', css) |
||||||
|
|
||||||
|
with open(css_filename, 'w') as css_file: |
||||||
|
css_file.write(css) |
@ -0,0 +1,28 @@ |
|||||||
|
import sys |
||||||
|
import subprocess |
||||||
|
import re |
||||||
|
|
||||||
|
extract_bookmarks_from = sys.argv[1] |
||||||
|
source_pdf_filename = sys.argv[2] |
||||||
|
output_filename = sys.argv[3] |
||||||
|
|
||||||
|
bookmarks_filename = extract_bookmarks_from.replace('.pdf', '.txt') |
||||||
|
assert(bookmarks_filename != extract_bookmarks_from) |
||||||
|
|
||||||
|
# extract PDF metadata into a text file |
||||||
|
subprocess.run(['pdftk', extract_bookmarks_from, 'dump_data', 'output', bookmarks_filename]) |
||||||
|
|
||||||
|
with open(bookmarks_filename) as bookmarks_file: |
||||||
|
metadata = bookmarks_file.read() |
||||||
|
|
||||||
|
# Offset page numbers |
||||||
|
def replaceBookmarkPageNumber(match): |
||||||
|
initial_page = int(match.group(1)) |
||||||
|
return 'BookmarkPageNumber: ' + str(initial_page + 2) |
||||||
|
metadata = re.sub('BookmarkPageNumber:\s+([0-9]+)', replaceBookmarkPageNumber, metadata) |
||||||
|
|
||||||
|
with open(bookmarks_filename, 'w') as bookmarks_file: |
||||||
|
bookmarks_file.write(metadata) |
||||||
|
|
||||||
|
# generate the output PDF |
||||||
|
subprocess.run(['pdftk', source_pdf_filename, 'update_info', bookmarks_filename, 'output', output_filename]) |
Loading…
Reference in new issue