"Fossies" - the Fresh Open Source Software Archive

Member "xhtml2pdf-0.2.2/xhtml2pdf/default.py" (16 Apr 2018, 12910 Bytes) of package /linux/www/xhtml2pdf-0.2.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "default.py" see the Fossies "Dox" file reference documentation.

    1 # -*- coding: utf-8 -*-
    2 from reportlab.lib.pagesizes import (A0, A1, A2, A3, A4, A5, A6, B0, B1, B2, B3,
    3                                      B4, B5, B6, LETTER, LEGAL, ELEVENSEVENTEEN)
    4 
    5 # Copyright 2010 Dirk Holtwick, holtwick.it
    6 #
    7 # Licensed under the Apache License, Version 2.0 (the "License");
    8 # you may not use this file except in compliance with the License.
    9 # You may obtain a copy of the License at
   10 #
   11 #     http://www.apache.org/licenses/LICENSE-2.0
   12 #
   13 # Unless required by applicable law or agreed to in writing, software
   14 # distributed under the License is distributed on an "AS IS" BASIS,
   15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   16 # See the License for the specific language governing permissions and
   17 # limitations under the License.
   18 
   19 PML_WARNING = "warning"
   20 PML_ERROR = "error"
   21 PML_EXCEPTION = "PML Exception"
   22 PML_PREFIX = "pdf:"
   23 
   24 #CLASS   = 1
   25 BOOL = 2
   26 FONT = 3
   27 COLOR = 4
   28 FILE = 5
   29 SIZE = 6
   30 INT = 7
   31 STRING = 8
   32 BOX = 9
   33 POS = 10
   34 #STYLE   = 11
   35 MUST = 23
   36 
   37 """
   38 Definition of all known tags. Also used for building the reference
   39 """
   40 
   41 TAGS = {
   42 
   43     # FORMAT
   44 
   45     #"document": (1, {
   46     #    "format":               (["a0", "a1", "a2", "a3", "a4", "a5", "a6",
   47     #                              "b0", "b1", "b2", "b3", "b4", "b5", "b6",
   48     #                              "letter", "legal", "elevenseventeen"], "a4"),
   49     #    "orientation":          ["portrait", "landscape"],
   50     #    "fullscreen":           (BOOL, "0"),
   51     #    "author":               (STRING, ""),
   52     #    "subject":              (STRING, ""),
   53     #    "title":                (STRING, ""),
   54     #    "duration":             INT,
   55     #    "showoutline":          (BOOL, "0"),
   56     #    "outline":              INT,
   57     #    }),
   58 
   59     "pdftemplate": (1, {
   60         "name": (STRING, "body"),
   61         "format": (["a0", "a1", "a2", "a3", "a4", "a5", "a6",
   62                     "b0", "b1", "b2", "b3", "b4", "b5", "b6",
   63                     "letter", "legal", "elevenseventeen"], "a4"),
   64         "orientation": ["portrait", "landscape"],
   65         "background": FILE,
   66     }),
   67 
   68     "pdfframe": (0, {
   69         "name": (STRING, ""),
   70         "box": (BOX, MUST),
   71         "border": (BOOL, "0"),
   72         "static": (BOOL, "0"),
   73     }),
   74 
   75     #"static": (1, {
   76     #    "name":                 STRING,
   77     #    "box":                  (BOX, MUST),
   78     #    "border":               (BOOL, "0"),
   79     #    }),
   80 
   81     "pdfnexttemplate": (0, {
   82         "name": (STRING, "body"),
   83     }),
   84 
   85     "pdfnextpage": (0, {
   86         "name": (STRING, ""),
   87         # "background":           FILE,
   88     }),
   89 
   90     "pdfnextframe": (0, {}),
   91 
   92     "pdffont": (0, {
   93         "src": (FILE, MUST),
   94         "name": (STRING, MUST),
   95         # "print":                (BOOL, "0"),
   96         "encoding": (STRING, "WinAnsiEncoding"),
   97     }),
   98 
   99     "pdfdrawline": (0, {
  100         "from": (POS, MUST),
  101         "to": (POS, MUST),
  102         "color": (COLOR, "#000000"),
  103         "width": (SIZE, 1),
  104     }),
  105 
  106     "drawpoint": (0, {
  107         "pos": (POS, MUST),
  108         "color": (COLOR, "#000000"),
  109         "width": (SIZE, 1),
  110     }),
  111 
  112     "pdfdrawlines": (0, {
  113         "coords": (STRING, MUST),
  114         "color": (COLOR, "#000000"),
  115         "width": (SIZE, 1),
  116     }),
  117 
  118     "pdfdrawstring": (0, {
  119         "pos": (POS, MUST),
  120         "text": (STRING, MUST),
  121         "color": (COLOR, "#000000"),
  122         "align": (["left", "center", "right"], "right"),
  123         "valign": (["top", "middle", "bottom"], "bottom"),
  124         # "class":                CLASS,
  125         "rotate": (INT, "0"),
  126     }),
  127 
  128     "pdfdrawimg": (0, {
  129         "pos": (POS, MUST),
  130         "src": (FILE, MUST),
  131         "width": SIZE,
  132         "height": SIZE,
  133         "align": (["left", "center", "right"], "right"),
  134         "valign": (["top", "middle", "bottom"], "bottom"),
  135     }),
  136 
  137     "pdfspacer": (0, {
  138         "height": (SIZE, MUST),
  139     }),
  140 
  141     "pdfpagenumber": (0, {
  142         "example": (STRING, "0"),
  143     }),
  144 
  145     "pdfpagecount": (0, {
  146     }),
  147 
  148     "pdftoc": (0, {
  149     }),
  150 
  151     "pdfversion": (0, {
  152     }),
  153 
  154     "pdfkeeptogether": (1, {
  155     }),
  156 
  157     "pdfkeepinframe": (1, {
  158         "maxwidth": SIZE,
  159         "maxheight": SIZE,
  160         "mergespace": (INT, 1),
  161         "mode": (["error", "overflow", "shrink", "truncate"], "shrink"),
  162         "name": (STRING, "")
  163     }),
  164 
  165     # The chart example, see pml_charts
  166     "pdfchart": (1, {
  167         "type": (["spider", "bar"], "bar"),
  168         "strokecolor": (COLOR, "#000000"),
  169         "width": (SIZE, MUST),
  170         "height": (SIZE, MUST),
  171     }),
  172 
  173     "pdfchartdata": (0, {
  174         "set": (STRING, MUST),
  175         "value": (STRING),
  176         # "label":                (STRING),
  177         "strokecolor": (COLOR),
  178         "fillcolor": (COLOR),
  179         "strokewidth": (SIZE),
  180     }),
  181 
  182     "pdfchartlabel": (0, {
  183         "value": (STRING, MUST),
  184     }),
  185 
  186     "pdfbarcode": (0, {
  187         "value": (STRING, MUST),
  188         "type": (["i2of5", "itf",
  189                   "code39", "extendedcode39",
  190                   "code93", "extendedcode93",
  191                   "msi",
  192                   "codabar", "nw7",
  193                   "code11",
  194                   "fim",
  195                   "postnet",
  196                   "usps4s",
  197                   "code128",
  198                   "ean13", "ean8",
  199                   "qr",
  200                  ], "code128"),
  201         "humanreadable": (STRING, "0"),
  202         "vertical": (STRING, "0"),
  203         "checksum": (STRING, "1"),
  204         "barwidth": SIZE,
  205         "barheight": SIZE,
  206         "fontsize": SIZE,
  207         "align": (["baseline", "top", "middle", "bottom"], "baseline"),
  208     }),
  209 
  210     # ========================================================
  211 
  212     "link": (0, {
  213         "href": (STRING, MUST),
  214         "rel": (STRING, ""),
  215         "type": (STRING, ""),
  216         "media": (STRING, "all"),
  217         "charset": (STRING, "latin1"), # XXX Must be something else...
  218     }),
  219 
  220     "meta": (0, {
  221         "name": (STRING, ""),
  222         "content": (STRING, ""),
  223     }),
  224 
  225     "style": (0, {
  226         "type": (STRING, ""),
  227         "media": (STRING, "all"),
  228     }),
  229 
  230     "img": (0, {
  231         "src": (FILE, MUST),
  232         "width": SIZE,
  233         "height": SIZE,
  234         "align": ["top", "middle", "bottom", "left", "right",
  235                   "texttop", "absmiddle", "absbottom", "baseline"],
  236     }),
  237 
  238     "table": (1, {
  239         "align": (["left", "center", "right"], "left"),
  240         "valign": (["top", "bottom", "middle"], "middle"),
  241         "border": (SIZE, "0"),
  242         "bordercolor": (COLOR, "#000000"),
  243         "bgcolor": COLOR,
  244         "cellpadding": (SIZE, "0"),
  245         "cellspacing": (SIZE, "0"),
  246         "repeat": (INT, "0"), # XXX Remove this! Set to 0
  247         "width": STRING,
  248         #"keepmaxwidth":         SIZE,
  249         #"keepmaxheight":        SIZE,
  250         #"keepmergespace":       (INT, 1),
  251         #"keepmode":             (["error", "overflow", "shrink", "truncate"], "shrink"),
  252     }),
  253 
  254     "tr": (1, {
  255         "bgcolor": COLOR,
  256         "valign": ["top", "bottom", "middle"],
  257         "border": SIZE,
  258         "bordercolor": (COLOR, "#000000"),
  259     }),
  260 
  261     "td": (1, {
  262         "align": ["left", "center", "right", "justify"],
  263         "valign": ["top", "bottom", "middle"],
  264         "width": STRING,
  265         "bgcolor": COLOR,
  266         "border": SIZE,
  267         "bordercolor": (COLOR, "#000000"),
  268         "colspan": INT,
  269         "rowspan": INT,
  270     }),
  271 
  272     "th": (1, {
  273         "align": ["left", "center", "right", "justify"],
  274         "valign": ["top", "bottom", "middle"],
  275         "width": STRING,
  276         "bgcolor": COLOR,
  277         "border": SIZE,
  278         "bordercolor": (COLOR, "#000000"),
  279         "colspan": INT,
  280         "rowspan": INT,
  281     }),
  282 
  283     "dl": (1, {
  284     }),
  285 
  286     "dd": (1, {
  287     }),
  288 
  289     "dt": (1, {
  290     }),
  291 
  292     "ol": (1, {
  293         "type": (["1", "a", "A", "i", "I"], "1"),
  294     }),
  295 
  296     "ul": (1, {
  297         "type": (["circle", "disk", "square"], "disk"),
  298     }),
  299 
  300     "li": (1, {
  301     }),
  302 
  303     "hr": (0, {
  304         "color": (COLOR, "#000000"),
  305         "size": (SIZE, "1"),
  306         "width": STRING,
  307         "align": ["left", "center", "right", "justify"],
  308     }),
  309 
  310     "div": (1, {
  311         "align": ["left", "center", "right", "justify"],
  312     }),
  313 
  314     "p": (1, {
  315         "align": ["left", "center", "right", "justify"],
  316     }),
  317 
  318     "br": (0, {
  319     }),
  320 
  321     "h1": (1, {
  322         "outline": STRING,
  323         "closed": (INT, 0),
  324         "align": ["left", "center", "right", "justify"],
  325     }),
  326 
  327     "h2": (1, {
  328         "outline": STRING,
  329         "closed": (INT, 0),
  330         "align": ["left", "center", "right", "justify"],
  331     }),
  332 
  333     "h3": (1, {
  334         "outline": STRING,
  335         "closed": (INT, 0),
  336         "align": ["left", "center", "right", "justify"],
  337     }),
  338 
  339     "h4": (1, {
  340         "outline": STRING,
  341         "closed": (INT, 0),
  342         "align": ["left", "center", "right", "justify"],
  343     }),
  344 
  345     "h5": (1, {
  346         "outline": STRING,
  347         "closed": (INT, 0),
  348         "align": ["left", "center", "right", "justify"],
  349     }),
  350 
  351     "h6": (1, {
  352         "outline": STRING,
  353         "closed": (INT, 0),
  354         "align": ["left", "center", "right", "justify"],
  355     }),
  356 
  357     "font": (1, {
  358         "face": FONT,
  359         "color": COLOR,
  360         "size": STRING,
  361     }),
  362 
  363     "a": (1, {
  364         "href": STRING,
  365         "name": STRING,
  366     }),
  367 
  368     "input": (0, {
  369         "name": STRING,
  370         "value": STRING,
  371         "type": (["text", "hidden", "checkbox"], "text"),
  372     }),
  373 
  374     "textarea": (1, {
  375         "name": STRING,
  376     }),
  377 
  378     "select": (1, {
  379         "name": STRING,
  380         "value": STRING,
  381     }),
  382 
  383     "option": (0, {
  384         "value": STRING,
  385     }),
  386 }
  387 
  388 # XXX use "html" not "*" as default!
  389 DEFAULT_CSS = """
  390 html {
  391     font-family: Helvetica;
  392     font-size: 10px;
  393     font-weight: normal;
  394     color: #000000;
  395     background-color: transparent;
  396     margin: 0;
  397     padding: 0;
  398     line-height: 150%;
  399     border: 1px none;
  400     display: inline;
  401     width: auto;
  402     height: auto;
  403     white-space: normal;
  404 }
  405 
  406 b,
  407 strong {
  408     font-weight: bold;
  409 }
  410 
  411 i,
  412 em {
  413     font-style: italic;
  414 }
  415 
  416 u {
  417     text-decoration: underline;
  418 }
  419 
  420 s,
  421 strike {
  422     text-decoration: line-through;
  423 }
  424 
  425 a {
  426     text-decoration: underline;
  427     color: blue;
  428 }
  429 
  430 ins {
  431     color: green;
  432     text-decoration: underline;
  433 }
  434 del {
  435     color: red;
  436     text-decoration: line-through;
  437 }
  438 
  439 pre,
  440 code,
  441 kbd,
  442 samp,
  443 tt {
  444     font-family: "Courier New";
  445 }
  446 
  447 h1,
  448 h2,
  449 h3,
  450 h4,
  451 h5,
  452 h6 {
  453     font-weight:bold;
  454     -pdf-outline: true;
  455     -pdf-outline-open: false;
  456 }
  457 
  458 h1 {
  459     /*18px via YUI Fonts CSS foundation*/
  460     font-size:138.5%;
  461     -pdf-outline-level: 0;
  462 }
  463 
  464 h2 {
  465     /*16px via YUI Fonts CSS foundation*/
  466     font-size:123.1%;
  467     -pdf-outline-level: 1;
  468 }
  469 
  470 h3 {
  471     /*14px via YUI Fonts CSS foundation*/
  472     font-size:108%;
  473     -pdf-outline-level: 2;
  474 }
  475 
  476 h4 {
  477     -pdf-outline-level: 3;
  478 }
  479 
  480 h5 {
  481     -pdf-outline-level: 4;
  482 }
  483 
  484 h6 {
  485     -pdf-outline-level: 5;
  486 }
  487 
  488 h1,
  489 h2,
  490 h3,
  491 h4,
  492 h5,
  493 h6,
  494 p,
  495 pre,
  496 hr {
  497     margin:1em 0;
  498 }
  499 
  500 address,
  501 blockquote,
  502 body,
  503 center,
  504 dl,
  505 dir,
  506 div,
  507 fieldset,
  508 form,
  509 h1,
  510 h2,
  511 h3,
  512 h4,
  513 h5,
  514 h6,
  515 hr,
  516 isindex,
  517 menu,
  518 noframes,
  519 noscript,
  520 ol,
  521 p,
  522 pre,
  523 table,
  524 th,
  525 tr,
  526 td,
  527 ul,
  528 li,
  529 dd,
  530 dt,
  531 pdftoc {
  532     display: block;
  533 }
  534 
  535 table {
  536 }
  537 
  538 tr,
  539 th,
  540 td {
  541 
  542     vertical-align: middle;
  543     width: auto;
  544 }
  545 
  546 th {
  547     text-align: center;
  548     font-weight: bold;
  549 }
  550 
  551 center {
  552     text-align: center;
  553 }
  554 
  555 big {
  556     font-size: 125%;
  557 }
  558 
  559 small {
  560     font-size: 75%;
  561 }
  562 
  563 
  564 ul {
  565     margin-left: 1.5em;
  566     list-style-type: disc;
  567 }
  568 
  569 ul ul {
  570     list-style-type: circle;
  571 }
  572 
  573 ul ul ul {
  574     list-style-type: square;
  575 }
  576 
  577 ol {
  578     list-style-type: decimal;
  579     margin-left: 1.5em;
  580 }
  581 
  582 pre {
  583     white-space: pre;
  584 }
  585 
  586 blockquote {
  587     margin-left: 1.5em;
  588     margin-right: 1.5em;
  589 }
  590 
  591 noscript {
  592     display: none;
  593 }
  594 """
  595 
  596 DEFAULT_FONT = {
  597     "courier": "Courier",
  598     "courier-bold": "Courier-Bold",
  599     "courier-boldoblique": "Courier-BoldOblique",
  600     "courier-oblique": "Courier-Oblique",
  601     "helvetica": "Helvetica",
  602     "helvetica-bold": "Helvetica-Bold",
  603     "helvetica-boldoblique": "Helvetica-BoldOblique",
  604     "helvetica-oblique": "Helvetica-Oblique",
  605     "times": "Times-Roman",
  606     "times-roman": "Times-Roman",
  607     "times-bold": "Times-Bold",
  608     "times-boldoblique": "Times-BoldOblique",
  609     "times-oblique": "Times-Oblique",
  610     "symbol": "Symbol",
  611     "zapfdingbats": "ZapfDingbats",
  612     "zapf-dingbats": "ZapfDingbats",
  613 
  614     # Alias
  615     "arial": "Helvetica",
  616     "times new roman": "Times-Roman",
  617     "georgia": "Times-Roman",
  618     'serif': 'Times-Roman',
  619     'sansserif': 'Helvetica',
  620     'sans': 'Helvetica',
  621     'monospaced': 'Courier',
  622     'monospace': 'Courier',
  623     'mono': 'Courier',
  624     'courier new': 'Courier',
  625     'verdana': 'Helvetica',
  626     'geneva': 'Helvetica',
  627 }
  628 
  629 PML_PAGESIZES = {
  630     "a0": A0,
  631     "a1": A1,
  632     "a2": A2,
  633     "a3": A3,
  634     "a4": A4,
  635     "a5": A5,
  636     "a6": A6,
  637     "b0": B0,
  638     "b1": B1,
  639     "b2": B2,
  640     "b3": B3,
  641     "b4": B4,
  642     "b5": B5,
  643     "b6": B6,
  644     "letter": LETTER,
  645     "legal": LEGAL,
  646     "ledger": ELEVENSEVENTEEN,
  647     "elevenseventeen": ELEVENSEVENTEEN,
  648 }