/
manual.txt
1890 lines (1277 loc) · 56.7 KB
/
manual.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
==================
How to use rst2pdf
==================
:author: Roberto Alsina <ralsina@netmanagers.com.ar>
:version: 0.93
:revision: $LastChangedRevision$
.. header::
.. oddeven::
.. class:: headertable
+---+---------------------+----------------+
| |.. class:: centered |.. class:: right|
| | | |
| |Section ###Section###|Page ###Page### |
+---+---------------------+----------------+
.. class:: headertable
+---------------+---------------------+---+
| |.. class:: centered | |
| | | |
|Page ###Page###|Section ###Section###| |
+---------------+---------------------+---+
.. contents::
.. section-numbering::
.. raw:: pdf
PageBreak oneColumn
Introduction
============
This document explains how to use rst2pdf. Here is the very short version::
rst2pdf.py mydocument.txt -o mydocument.pdf
That will, as long as mydocument.txt is a valid Restructured Text (ReST)
document, produce a file called mydocument.pdf which is a PDF
version of your document.
Of course, that means you just used default styles and settings. If it
looks good enough for you, then you may stop reading this document,
because you are done with it. If you are reading this in a PDF,
it was generated using those default settings.
However, if you want to customize the output, or are just curious to see
what can be done, let´s continue.
Command line options
====================
.. include:: rst2pdf.txt
:start-line: 30
:end-before: EXAMPLES
Configuration File
==================
Since version 0.8, rst2pdf will read (if it is available) configuration files in
``/etc/rst2pdf.conf`` and ``~/.rst2pdf/config``.
The user's file at ``~/.rst2pdf/config`` will have priority over the system's at
``/etc/rst2pdf.conf`` [#]_
.. [#] The ``/etc/rst2pdf.conf`` location makes sense for Linux and linux-like systems.
if you are using rst2pdf in other systems, please contact me and tell me where
the system-wide config file should be.
Here's an example file showing some of the currently available options:
.. code-block:: ini
:include: config.sample
Pipe usage
==========
If no input nor output are provided, stdin and stdout will be used respectively
You may want to use rst2pdf in a linux pipe as such::
cat readme.txt | rst2pdf | gzip -c > readme.pdf.gz
or::
curl http://docutils.sourceforge.net/docs/user/rst/quickstart.txt | rst2pdf > quickstart.pdf
If no input argument is provided, stdin will be used::
cat readme.txt | rst2pdf -o readme.pdf
If outpufile is set to dash '-', output goes to stdout::
rst2pdf -o - readme.txt > output.pdf
Headers and Footers
===================
ReST supports headers and footers, using the header and footer directive::
.. header::
This will be at the top of every page.
Often, you may want to put a page number there, or a section name.The following
magic tokens will be replaced (More may be added as rst2pdf evolves):
###Page###
Replaced by the current page number.
###Title###
Replaced by the document title
###Section###
Replaced by the currect section title
###SectNum###
Replaced by the currect section number. **Important:** You must use the sectnum directive for this to work.
###Total###
Replaced by the total number of pages in the document. Keep in mind that this is the **real** number of pages, not the displayed number, so if you play with `page counters`_ this number will probably be wrong.
Headers and footers are visible by default but they can be disabled by specific
`Page Templates`_ for example, cover pages. You can also set headers and footers
via `command line options` or the `configuration file`_.
If you want to do things like "put the page number on the *out* side of the page, check `The oddeven directive`_
Footnotes
=========
Currently rst2pdf doesn't support real footnotes, and converts them to endnotes.
There is a real complicated technical reason for this: I can't figure out a
clean way to do it right.
You can get the same behaviour as with rst2html by specifying --inline-footnotes,
and then the footnotes will appear where you put them (in other words, not footnotes,
but "in-the-middle-of-text-notes" or just plain notes.)
Images
======
Inline
------
You can insert images in the middle of your text like this::
This |biohazard| means you have to run.
.. |biohazard| image:: ../rst2pdf/tests/input/images/biohazard.png
This |biohazard| means you have to run.
.. |biohazard| image:: ../rst2pdf/tests/input/images/biohazard.png
This only works correctly with reportlab 2.2 or later.
Supported Image Types
---------------------
For raster images, rst2pdf supports anything PIL (The Python Imaging Library) supports.
The exact list of supported formats varies according to your PIL version and system.
For SVG support, you need to install svg2rlg or use the inkscape extension.
Some features will not work when using these images.For example, gradients will not
display, and text may cause problems depending on font availability.
You can also use PDF images, via pdfrw.
If you can choose between raster and vectorial images, for non-photographic images,
vector files are usually smaller and look better, specially when printed.
.. note:: Image URLs
Attempting to be more compatible with rst2html, rst2pdf will try
to handle images specified as HTTP or FTP URLs by downloading them
to a temporary file and including them in the PDF.
This is probably not a good idea unless you are **really** sure
the image won't go away.
Image Size
----------
PDFs are meant to reflect paper. A PDF has a specific size in centimeters or inches.
Images usually are measured in pixels, which are meaningless in a PDF. To convert
between pixels and inches or centimeters, we use a DPI (dots-per-inch) value.
For example, 300 pixels, with a 300DPI, are exactly one inch. 300 pixels at 100DPI
are 3 inches.
For that reason, to achieve a nice layout of the page, it's usually a good idea
to specify the size of your images in those units, or as a percentage of the
available width and you can ignore all this DPI nonsense ;-)
The rst2pdf default is 300DPI, but you can change it using the --default-dpi option
or the default_dpi setting in the config file.
Examples of images with specified sizes::
.. image:: home.png
:width: 3in
.. image:: home.png
:width: 80%
.. image:: home.png
:width: 7cm
The valid units you can use are:
"em" "ex" "px" "in" "cm" "mm" "pt" "pc" "%" "".
* px: Pixels. If you specify the size using this unit, rst2pdf will convert it to
inches using the default DPI explained above.
* No unit. If you just use a number, it will be considered as pixels. (**IMPORTANT:**
this used to default to points. It was changed to be more compatible with rst2html)
* em: This is the same as your base style's font size. By default: 10 points.
* ex: rst2pdf will use the same broken definition as IE: em/2. In truth this should
be the height of the lower-case x character in your base style.
* in: Inches (1 inch = 2.54 cm).
* cm: centimeters (1cm = 0.39 inches)
* mm: millimeters (10mm = 1cm)
* pt: 1/72 inch
* pc: 1/6 inch
* %: percentage of available width in the frame. Setting a percentage as a height
does **not** work and probably never will.
If you don't specify a size at all, rst2pdf will do its best to figure out what it should do:
Since there is no specified size, rst2pdf will try to convert the image's pixel size to
inches using the DPI information available in the image itself. You can set that value
using most image editors. For example, using Gimp, it's in the Image -> Print Size menu.
So, if your image is 6000 pixels wide, and is set to 1200DPI, it will be 5 inches wide.
If your image doesn't have a DPI property set, and doesn't have it's desired size specified,
rst2pdf will arbitrarily decide it should use 300DPI (or whatever you choose with
the --default-dpi option).
Styles
======
You can style paragraphs with a style using the class directive::
.. class:: special
This paragraph is special.
This one is not.
Or inline styles using custom interpreted roles::
.. role:: redtext
I like color :redtext:`red`.
For more information about this, please check the ReST docs.
The only special thing about using rst2pdf here is the syntax of
the stylesheet.
You can make rst2pdf print the default stylesheet::
rst2pdf --print-stylesheet
If you want to add styles, just create a stylesheet, (or take the standard
stylesheet and modify it) and pass it with the -s option::
rst2pdf mydoc.txt -s mystyles.txt
Those styles will always be searched in these places, in order:
* What you specify using --stylesheet_path
* The option stylesheet_path in the config file
* The current folder
* ~/.rst2pdf/styles
* The styles folder within rst2pdf's installation folder.
You can use multiple -s options, or pass more than one stylesheet
separated with commas. They are processed in the order you give them
so the *last* one has priority.
Included StyleSheets
--------------------
To make some of the more common adjustments easier, rst2pdf includes a
collection of stylesheets you can use:
Font styles
These stylesheets modfy your font settings.
* ``serif`` uses the PDF serif font (Times) instead of the default Sans
Serif (Arial)
* ``freetype-sans`` uses your system's default TrueType Sans Serif font
* ``freetype-serif`` uses your system's default TrueType Serif font
* ``twelvepoint`` makes the base font 12pt (default is 10pt)
* ``tenpoint`` makes the base font 10pt
* ``eightpoint`` makes the base font 8pt
* ``kerning`` switches to document to DejaVu Sans font and enables kerning.
Page layout styles
These stylesheets modify your page layout.
* ``twocolumn`` uses the twoColumn layout as the initial page layout.
* ``double-sided`` adds a gutter margin (margin at the "in side" of the pages)
Page size styles
Stylesheets that change the paper size.
The usual standard paper sizes are supported:
* A0
* A1
* A2
* A3
* A4 (default)
* A5
* A6
* B0
* B1
* B2
* B3
* B4
* B5
* B6
* Letter
* Legal
* 11x17
The name of the stylesheet is lowercase.
Code block styles
See `Syntax Highlighting`_
So, if you want to have a two-column, legal size, serif document with code in murphy style::
rst2pdf mydoc.txt -s twocolumn,serif,murphy,legal
StyleSheet Syntax
-----------------
It´s a JSON file with several elements in it.
Font Alias
----------
This is the fontsAlias element. By default, it uses some of the standard PDF fonts::
"fontsAlias" : {
"stdFont": "Helvetica",
"stdBold": "Helvetica-Bold",
"stdItalic": "Helvetica-Oblique",
"stdBoldItalic": "Helvetica-BoldOblique",
"stdMono": "Courier"
},
This defines the fonts used in the styles. You can use, for example, Helvetica
directly in a style, but if later you want to use another font all through
your document, you will haveto change it in each style. So, I suggest you
use aliases.
The standard PDF fonts are these:
Times_Roman
Times-Bold
Times-Italic
Times-Bold-Italic
Helvetica
Helvetica_Bold
Helvetica-Oblique
Helvetica-Bold-Oblique
Courier
Courier-Bold
Courier-Oblique
Courier-Bold-Oblique
Symbol
Zapf-Dingbats
Style Definition
----------------
Then you have a 'styles' which is a list of [ stylename, styleproperties ]. For example::
["normal" , {
"parent": "base"
}],
This means that the style called "normal" inherits style "base". So, each property
not defined in the normal style will be taken from the base style.
I suggest you do not remove any style from the default stylesheet. Add or modify at
will, though.
If your document requires a style that is not defined in your stylesheet, it will
print a warning and use bodytext instead.
Also, the order of the styles is important: if styleA is the parent of styleB,
styleA should be earlier in the stylesheet.
These are all the possible attributes for a style and their default values.
Some of them, like alignment, apply only when used to paragraphs,
and not on inline styles::
"fontName":"Helvetica",
"fontSize":10,
"leading":12,
"leftIndent":0,
"rightIndent":0,
"firstLineIndent":0,
"alignment":"left",
"spaceBefore":0,
"spaceAfter":0,
"bulletFontName":"Helvetica",
"bulletFontSize":10,
"bulletText": "\u2022",
"bulletIndent":0,
"textColor": black,
"backColor":None,
"wordWrap":None,
"borderWidth": 0,
"borderPadding": 0,
"borderColor": None,
"borderRadius": None,
"allowWidows": 5,
"allowOrphans": 4
The following are the only attributes that work on styles when used for interpreted roles
(inline styles):
* fontName
* fontSize
* textColor
* backColor (if your reportlab is version 2.3 or newer)
Widows and Orphans
------------------
Widow
A paragraph-ending line that falls at the beginning of the following page/column, thus separated from the remainder of the text.
Orphan
A paragraph-opening line that appears by itself at the bottom of a page/column.
Rst2pdf has *some* widow/orphan control. Specifically, here's what's currently implemented:
On ordinary paragraphs, allowWidows ans allowOrphans is passed to reportlab, which is supposed to do something about it if they are non-zero. In practice, it doesn't seem to have much effect.
The plan is to change the semantics of those settings, so that they mean the minimum number of lines that can be left alone at the beginning of a page (widows) or at the end (orphans).
Currently, these semantics only work for literal blocks and code blocks.
.. code-block:: rst
A literal block::
This is a literal block.
A code block:
.. code-block:: python
def x(y):
print y**2
In future versions this may extend to ordinary paragraphs.
Font Embedding
--------------
There are thousands of excelent free True Type and Type 1 fonts available on the
web, and you can use many of them in your documents by declaring them in your
stylesheet.
The Easy Way
~~~~~~~~~~~~
Just use the font name in your style. For example, you can define this::
["normal" , {
"fontName" : "fonty"
}]
And then it *may* work.
What would need to happen for this to work?
Fonty is a True Type font:
""""""""""""""""""""""""""
1. You need to have it installed in your system, and have the fc-match
utility available (it's part of fontconfig_). You can test if it is
so by running this command::
$ fc-match fonty
fonty.ttf: "Fonty" "Normal"
If you are in Windows, I need your help ;-) or you can use `The Harder Way (True Type)`_
2. The folder where fonty.ttf is located needs to be in your font path. You can set it
using the --font-path option. For example::
rst2pdf mydoc.txt -s mystyle.style --font-path /usr/share/fonts
You don't need to put the *exact* folder, just something that is above it. In my own case,
fonty is in /usr/share/fonts/TTF
Whenever a font is embedded, you can refer to it in a style by its name, and
to its variants by the aliases Name-Oblique, Name-Bold, Name-BoldOblique.
Fonty is a Type 1 font:
"""""""""""""""""""""""
You need it installed, and the folders where its font metric (.afm) and binary (.pfb) files
are located need to be in your font fath.
For example, the "URW Palladio L" font that came with my installation of TeX consists of
the following files::
/usr/share/texmf-dist/fonts/type1/urw/palatino/uplb8a.pfb
/usr/share/texmf-dist/fonts/type1/urw/palatino/uplbi8a.pfb
/usr/share/texmf-dist/fonts/type1/urw/palatino/uplr8a.pfb
/usr/share/texmf-dist/fonts/type1/urw/palatino/uplri8a.pfb
/usr/share/texmf-dist/fonts/afm/urw/palatino/uplb8a.afm
/usr/share/texmf-dist/fonts/afm/urw/palatino/uplbi8a.afm
/usr/share/texmf-dist/fonts/afm/urw/palatino/uplr8a.afm
/usr/share/texmf-dist/fonts/afm/urw/palatino/uplri8a.afm
So, I can use it if I put ``/usr/share/texmf-dist/fonts`` in my font path::
rst2pdf mydoc.txt -s mystyle.style --font-path /usr/share/texmf-dist/fonts
And putting this in my stylesheet, for example::
[ "title", { "fontName" : "URWPalladioL-Bold" } ]
There are some standard aliases defined so you can use other names::
'ITC Bookman' : 'URW Bookman L',
'ITC Avant Garde Gothic' : 'URW Gothic L',
'Palatino' : 'URW Palladio L',
'New Century Schoolbook' : 'Century Schoolbook L',
'ITC Zapf Chancery' : 'URW Chancery L'
So, for example, you can use ``Palatino`` or ``New Century SchoolBook-Oblique`` And it will mean
``URWPalladioL`` or ``CenturySchL-Ital``, respectively.
Whenever a font is embedded, you can refer to it in a style by its name, and
to its variants by the aliases Name-Oblique, Name-Bold, Name-BoldOblique.
The Harder Way (True Type)
~~~~~~~~~~~~~~~~~~~~~~~~~~
The stylesheet has an element is "embeddedFonts" that handles embedding True Type
fonts in your PDF.
Usually, it's empty, because with the default styles you are not using any font
beyond the standard PDF fonts::
"embeddedFonts" : [ ],
You can put there the name of the font, and rst2pdf will try to embed it as described
above. Example::
"embeddedFonts" : [ "Tuffy" ],
Or you can be explicit and tell rst2pdf the files that contain each variant of the
font.
Suppose you want to use the nice public domain `Tuffy font`_, then you need to give the
filenames of all variants::
"embeddedFonts" : [ ["Tuffy.ttf","Tuffy_Bold.ttf","Tuffy_Italic.ttf","Tuffy_Bold_Italic.ttf"] ],
This will provide your styles with fonts called "Tuffy" "Tuffy_Bold" and so on. They will
be available with the names based on the filenames (Tuffy_Bold) and also by standard
aliases similar to those of the standard PDF fonts (Tuffy-Bold/Tuffy-Oblique/Tuffy-BoldOblique).
Now, if you use *italics* in a paragraph whose style uses the Tuffy font, it will use
Tuffy_Italic. That's why it's better if you use fonts that provide the four variants, and
you should put them in **that** order. If your font lacks a variant, use the
"normal" variant instead.
For example, if you only had Tuffy.ttf::
"embeddedFonts" : [ ["Tuffy.ttf","Tuffy.ttf","Tuffy.ttf","Tuffy.ttf"] ],
However, that means that italics and bold in styles using Tuffy will not work correctly (they will
display as regular text).
If you want to use this as the base font for your document, you should change the fontsAlias
section accordingly. For example::
"fontsAlias" : {
"stdFont": "Tuffy",
"stdBold": "Tuffy_Bold",
"stdItalic": "Tuffy_Italic",
"stdBoldItalic": "Tuffy_Bold_Italic",
"stdMono": "Courier"
},
If, on the other hand, you only want a specific style to use the Tuffy
font, don't change the fontAlias, and set the fontName properties for
that style. For example::
["heading1" , {
"parent": "normal",
"fontName": "Tuffy_Bold",
"fontSize": 18,
"keepWithNext": true,
"spaceAfter": 6
}],
.. _tuffy font: http://tulrich.com/fonts/
By default, rst2pdf will search for the fonts in its fonts folder and
in the current folder. You can make it search another folder by passing
the --font-folder option, or you can use absolute paths in your stylesheet.
The Harder Way (Type1)
~~~~~~~~~~~~~~~~~~~~~~
To be written (and implemented and tested)
Page Size and Margins
---------------------
In your stylesheet, the pageSetup element controls your page layout.
Here's the default stylesheet's::
"pageSetup" : {
"size": "A4",
"width": null,
"height": null,
"margin-top": "2cm",
"margin-bottom": "2cm",
"margin-left": "2cm",
"margin-right": "2cm",
"spacing-header": "5mm",
"spacing-footer": "5mm",
"margin-gutter": "0cm"
},
Size is one of the standard paper sizes, like A4 or LETTER.
Here's a list: A0, A1, A2, A3, A4, A5, A6, B0, B1, B2, B3, B4, B5, B6,
LETTER, LEGAL, ELEVENSEVENTEEN.
If you want a non-standard size, set size to null and use width and height.
When specifying width, height or margins, you need to use units, like
inch (inches) or cm (centimeters).
When both width/height and size are specified, size will be used, and
width/height ignored.
All margins should be self-explanatory, except for margin-gutter. That's the
margin in the center of a two-page spread.
This value is added to the left margin of odd pages and the right margin of
even pages, adding (or removing, if it's negative) space "in the middle" of
opposingpages.
If you intend to bound a printed copy, you may need extra space there. OTOH,
if you will display it on-screen on a two-page format (common in many PDF
readers, nice for ebooks), a negative value may be pleasant.
Advanced: table styles
----------------------
This is new in 0.12.
These are a few extra options in styles that are only used when the style is applied to a table.
This happens in two cases:
1) You are using the class directive on a table:
.. code-block:: rest
.. class:: thick
+-------+---------+
| A | B |
+-----------------+
2) It's a style that automatically applies to something that is *drawn* using a table.
Currently these include:
* Footnotes / endnotes (endnote style)
* Lists (item_list, bullet_list option_list and field_list styles)
The options are as follows:
Commands
For a full reference of these, please check the Reportlab User Guide
specifically the TableStyle Commands section (section 7.4 in the manual
for version 2.3)
Here, however, is a list of the possible commands::
BOX (or OUTLINE)
FONT
FONTNAME (or FACE)
FONTSIZE (or SIZE)
GRID
INNERGRID
LEADING
LINEBELOW
LINEABOVE
LINEBEFORE
LINEAFTER
TEXTCOLOR
ALIGNMENT (or ALIGN)
LEFTPADDING
RIGHTPADDING
BOTTOMPADDING
TOPPADDING
BACKGROUND
ROWBACKGROUNDS
COLBACKGROUNDS
VALIGN
Each takes as argument a couple of coordinates, where (0,0) is top-left, and (-1,-1) is
bottom-right, and 0 or more extra arguments.
For example, INNERGRID takes a linewidth and a color::
[ "INNERGRID", [ 0, 0 ], [ -1, -1 ], 0.25, "black" ],
That would mean "draw all lines inside the table with .25pt black"
colWidths
A list of the column widths you want, in the unit you prefer (default unit is pt).
Example::
"colWidths": ["3cm",null]
If your colWidths has fewer values than columns in your table, the rest are autocalculated.
A column width of null means "guess".
If you don't specify column widths, the table will try to look proportional to the restructured
text source.
Multiple Stylesheets
--------------------
When you use a custom stylesheet, you don't need to define *everything* in it.
Whatever you don't define will be taken from the default stylesheet. For example,
if you only want to change page size, default font and font size, this would
be enough:
.. code-block:: js
{
"pageSetup" : {
"size": "A5",
},
"fontsAlias" : {
"stdFont": "Times-Roman",
},
"styles" : [
["normal" , {
"fontSize": 14
}]
]
}
.. Note::
The ``command`` option used for table styles is not kept across stylesheets.
For example, the default stylesheet defines endnote with this command list::
"commands": [ [ "VALIGN", [ 0, 0 ], [ -1, -1 ], "TOP" ] ]
If you redefine endnote in another stylesheet and use this to create a vertical line between
the endnote's columns::
"commands": [ [ "LINEAFTER", [ 0, 0 ], [ 1, -1 ], .25, "black" ] ]
Then the footnotes will **not** have VALIGN TOP!
To do that, you **MUST** use all commands in your stylesheet::
"commands": [
[ "VALIGN", [ 0, 0 ], [ -1, -1 ], "TOP" ],
[ "LINEAFTER", [ 0, 0 ], [ 1, -1 ], .25, "black" ]
]
.. raw:: pdf
PageBreak
Styling Your Document
---------------------
Which styles you need to modify to achieve your desired result is not obvious.
In this section, you will see some hints and pointers to that effect.
The Base Styles
~~~~~~~~~~~~~~~
There are three styles which have great effect, they are ``base``,
``normal`` and ``bodytext``.
Here's an example, the twelvepoint stylesheet::
{"styles": [["base", {"fontSize": 12}]]}
Since all other styles inherit ``base``, changing the fontSize changes the fontSize
for everything in your document.
The ``normal`` style is meant for most elements, so usually it's the same as changing
``base``.
The ``bodytext`` style is for elements that form paragraphs. So, for example, you can set
your document to be left-aligned like this::
{"styles": [["bodytext", {"alignment": "left"}]]}
There are elements, however, that don't inherit from bodytext, for example headings and the
styles used in the table of contents. Those are elements that are not real paragraphs, so
they should not follow the indentation and spacing you use for your document's main content.
The ``heading`` style is inherited by all sorts of titles: section titles, topic titles,
admonition titles, etc.
Lists
~~~~~
Styling lists is mostly a matter of spacing and indentation.
The space before and after a list is taken from the ``item_list`` and ``bullet_list`` styles::
["item_list", {
"parent": "bodytext",
"spaceBefore": 0,
"commands": [
[ "VALIGN", [ 0, 0 ], [ -1, -1 ], "TOP" ],
[ "RIGHTPADDING", [ 0, 0 ], [ 1, -1 ], 0 ]
],
"colWidths": ["20pt",null]
}]
["bullet_list", {
"parent": "bodytext",
"spaceBefore": 0,
"commands": [
[ "VALIGN", [ 0, 0 ], [ -1, -1 ], "TOP" ],
[ "RIGHTPADDING", [ 0, 0 ], [ 1, -1 ], 0 ]
],
"colWidths": ["20",null]
}],
Yes, these are table styles, because they are implemented as tables. The RIGHTPADDING command
and the colWidths option can be used to adjust the position of the bullet/item number.
To control the separation between items, you use the item_list_item and
``bullet_list_item`` styles' spaceBefore and spaceAfter options, for example::
["bullet_list_item" , {
"parent": "bodytext",
"spaceBefore": 20
}]
Remember that this is only used **between items** and not before the first or after the
last items.
Syntax Highlighting
===================
Rst2pdf adds a non-standard directive, called code-block, which produces syntax
highlighted for many languages using Pygments_.
For example, if you want to include a python fragment::
.. code-block:: python
def myFun(x,y):
print x+y
.. code-block:: python
def myFun(x,y):
print x+y
Notice that you need to declare the language of the fragment. Here's a list of
the currently supported_.
You can use the ``linenos`` option to display line numbers:
.. code-block:: python
:linenos:
def myFun(x,y):
print x+y
You can use the ``hl_lines`` option to emphasize certain lines by dimming the
other lines. This parameter takes a space separated list of line numbers. The
other lines are then styled with the class ``pygments_diml`` that defaults to
gray. e.g. to highlight ``print "line a"`` and ``print "line b"``:
.. code-block:: python
:hl_lines: 2 3
def myFun(x,y):
print "line a"
print "line b"
print "line c"
Rst2pdf includes several stylesheets for highlighting code:
* autumn
* borland
* bw
* colorful
* emacs
* friendly
* fruity
* manni
* murphy
* native
* pastie
* perldoc
* trac
* vs
You can use any of them instead of the default by adding, for example, a ``-s murphy`` to
the command line.
If you already are using a custom stylesheet, use both::
rst2pdf mydoc.rst -o mydoc.pdf -s mystyle.json,murphy
The default is the same as "emacs".
There is an online demo of pygments showing these styles:
http://pygments.org/demo/1817/
The overal look of a code box is controlled by the "code" style or by a class you apply to it using the ``.. class::`` directive.
Aditionally, if you want to change some properties when using different languages, you can define styles with the name of the language.
For example, a ``python`` style will be applied to code blocks created with ``.. code-block:: python``.
The look of the line numbers is controlled by the "linenumbers" style.
As rst2pdf is written in python let's see some examples and variations around python.
Python in console
.. code-block:: pycon
>>> my_string="python is great"
>>> my_string.find('great')
10
>>> my_string.startswith('py')
True
Python traceback
.. code-block:: pytb
Traceback (most recent call last):
File "error.py", line 9, in ?
main()
File "error.py", line 6, in main
print call_error()