Skip to content

Commit

Permalink
resolves asciidoctor#749 allow page margin to be set using pdf-page-m…
Browse files Browse the repository at this point in the history
…argin attribute
  • Loading branch information
mojavelinux committed Mar 27, 2017
1 parent e1a6a6f commit be52a87
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
5 changes: 4 additions & 1 deletion docs/theming-guide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ ifdef::backend-pdf[:conum-guard-yaml: # #]

////
Topics remaining to document:
* document which attributes can be set in document (pdf-page-size, front-cover-image, back-cover-image, etc) (issue #428)
* line height and line height length (and what that all means)
* title page layout / title page images (logo & background)
* document that unicode escape sequences can be used inside double-quoted strings
Expand Down Expand Up @@ -3397,6 +3396,10 @@ These settings override equivalent keys defined in the theme file, where applica
|portrait {vbar} landscape
|:pdf-page-layout: landscape

|pdf-page-margin
|<<measurement-units,Measurement>> {vbar} <<measurement-units,Measurement[top,right,bottom,left]>>
|:pdf-page-margin: [1in, 0.5in]

|pdf-page-size
|https://github.com/prawnpdf/pdf-core/blob/0.6.0/lib/pdf/core/page_geometry.rb#L16-L68[Named size^] {vbar} <<measurement-units,Measurement[width, height]>>
|:pdf-page-size: 6in x 9in
Expand Down
27 changes: 25 additions & 2 deletions lib/asciidoctor-pdf/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,29 @@ def init_pdf doc
end

def build_pdf_options doc, theme
case (page_margin = (doc.attr 'pdf-page-margin') || theme.page_margin)
when ::String
if page_margin.empty?
page_margin = nil
elsif (page_margin.start_with? '[') && (page_margin.end_with? ']')
if (page_margin = page_margin[1...-1].rstrip).empty?
page_margin = [0]
else
if (page_margin = page_margin.split ',', -1).length > 4
page_margin = page_margin[0..3]
end
page_margin = page_margin.map {|v| str_to_pt v.rstrip }
end
else
page_margin = [(str_to_pt page_margin)]
end
when ::Array
page_margin = page_margin[0..3] if page_margin.length > 4
page_margin = page_margin.map {|v| ::Numeric === v ? v : (str_to_pt v.to_s) }
else
page_margin = nil
end

page_size = if (doc.attr? 'pdf-page-size') && (m = PageSizeRx.match(doc.attr 'pdf-page-size'))
# e.g, [8.5in, 11in]
if m[1]
Expand Down Expand Up @@ -325,10 +348,10 @@ def build_pdf_options doc, theme
{
#compress: true,
#optimize_objects: true,
info: (build_pdf_info doc),
margin: theme.page_margin,
margin: (page_margin || 36),
page_size: (page_size || 'A4'),
page_layout: (page_layout || :portrait),
info: (build_pdf_info doc),
skip_page_creation: true,
text_formatter: (FormattedText::Formatter.new theme: theme)
}
Expand Down

0 comments on commit be52a87

Please sign in to comment.