Skip to content

Instantly share code, notes, and snippets.

@Krutie
Last active September 30, 2024 13:01
Show Gist options
  • Save Krutie/3dec3ed026da6de662e77a7faa52f2c0 to your computer and use it in GitHub Desktop.
Save Krutie/3dec3ed026da6de662e77a7faa52f2c0 to your computer and use it in GitHub Desktop.
This script will modify how the title page, part-title, and chapter-title are rendered.
require 'asciidoctor-pdf'
class CustomPdfConverter < Asciidoctor::PDF::Converter
register_for 'pdf'
# Customize the title page layout
def layout_title_page(doc)
start_new_page
font 'Helvetica', size: 48, style: :bold
text doc.doctitle, align: :center, valign: :center
move_down 30
font 'Helvetica', size: 20
text doc.attr('author', 'Anonymous'), align: :center
move_down 10
text doc.attr('revdate', Time.now.strftime('%Y-%m-%d')), align: :center
move_down 20
text "Custom Title Page", align: :center
end
# Customize the part-title layout
def layout_part_title(part)
start_new_page
font 'Helvetica', size: 36, style: :bold
text part.title, align: :center, valign: :center
move_down 50
font 'Helvetica', size: 24
text "Part #{part.number}", align: :center
start_new_page
end
# Customize the chapter-title layout
def layout_chapter_title(chapter, opts = {})
# Start new chapter page
start_new_page unless at_page_top?
move_down 200 if chapter.number == 1 # Add top padding for the first chapter
# Set the custom title for the chapter
font 'Helvetica', size: 32, style: :bold
text chapter.title, align: :center
# Optional: Customize chapter number display
move_down 20
font 'Helvetica', size: 18, style: :italic
text "Chapter #{chapter.number}", align: :center
# Start new page after title if necessary
start_new_page
end
end

Once you have saved the script: custom_page_layout.rb , you can pass it to the asciidoctor-pdf command to generate the PDF with custom page layouts.

Run the following command:

asciidoctor-pdf -r ./custom_page_layout.rb my_document.adoc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment