Sample PDF handbook

• Oct 28, 2008 - 06:17

Attached is a sample handbook created using Python HTML2PDF converter:

Here's the style information I used:

      h1 {
        -pdf-outline: true;
        -pdf-level: 0;
        -pdf-open: true;
      }
      h2 {
        -pdf-outline: true;
        -pdf-level: 2;
        -pdf-open: false;
      }
      h3,h4,h5,h6 {-pdf-outline:false;}
      pdftoc {
        color: #666;
      }
      pdftoc.pdftoclevel0 {
        font-weight: bold;
        margin-top: 0.5em;
        border-bottom: .25pt dotted black;
      }
      pdftoc.pdftoclevel1 {
        margin-left: 1em;
      }
      pdftoc.pdftoclevel2 {
        margin-left: 2em;
        font-style: italic;
        display:none;
      } 
      @frame footer {
        -pdf-frame-content: footerContent;
        bottom: 1cm; border:1pt solid black;
        margin-left: 4cm;
        margin-right: 3cm;
        height: 1cm;
      }
      #footerContent {text-align:right;}


      img {margin-left: 5em; zoom: 50%; }  
      
      
      @page {margin: 2cm 3cm 2cm 4cm;}
      body {font-family:serif; font-size:10pt;}
      p {margin-left: 0em;}
      ul, ol, dl {margin:1em 0 1em 1em;}
      li, dd {margin:.2em 0 .2em 1em;}
      
      img+br,br+img {margin: .2em 0 .2em 0;}
      
      div div h1 {page-break-before:always;}
      h1, h2, h3, h4, h5, h6 {font-family:sans-serif}
      h1 {border-bottom:.25pt solid black;}
      h2 {margin-top:2.5em;}
      h3,h4,h5,h6 {margin:1em 0 0 0em; font-style:italic; color:#505050;}
      
      
      kbd.key {
        border: 1pt solid black;
        font-family:sans-serif;
        font-size:0.85em;
        font-style:italic;
        margin:0pt;
        padding:0pt 4pt;
      }
      samp.mode {
        font-family:sans-serif;
      }
      kbd samp {
        border:0pt none;
        font-family:sans-serif;
        margin:0pt;
        padding:1pt;
      }

I added the following javascript to the head of the HTML to better prepare it for PDF export. I then accessed the modified HTML via the "View Selection Source" feature in Firefox.

      window.onload = function() {
        
        //Give level 1 headings an anchor tag with a name attribute based on the heading text
        var split_body = document.getElementsByTagName("body")[0].innerHTML.split("<h1");
        for (i=1; i < split_body.length; i++) {
          name = split_body[i].substring(split_body[i].indexOf(">")+1,split_body[i].indexOf("</h1>")).toLowerCase().replace(/ /g,"-");
          split_body[i-1] = split_body[i-1] + "<a name=\"" + name + "\"></a>";
        }
        document.getElementsByTagName("body")[0].innerHTML = split_body.join("<h1");
        
        //Give level 3 headings an anchor tag with a name attribute based h3 id attribute
        var split_body = document.getElementsByTagName("body")[0].innerHTML.split("<h3 id=\"");
        for (i=1; i < split_body.length; i++) {
          name = split_body[i].substring(0,split_body[i].indexOf("\""));
          split_body[i-1] = split_body[i-1] + "<a name=\"" + name + "\"></a>";
        }
        document.getElementsByTagName("body")[0].innerHTML = split_body.join("<h3 id=\"");
        
        //Fix links so that they link to headings with in the document
        for (i=0; i < document.getElementsByTagName("a").length; i++) {
          var href = document.getElementsByTagName("a")[i].href;
          if (/http:.+freelinking\//.test(href)) {
            document.getElementsByTagName("a")[i].href=document.getElementsByTagName("a")[i].href.replace(/http:.+freelinking\//,"#").replace(/(%20)|(%2520)/g,"-");
          }
        }
        
        //Replace h1 tag with h2 tag for subheadings
        for (i = 0; i < document.getElementsByTagName("div").length; i++) {
          var div = document.getElementsByTagName("div")[i];
          if (div.parentNode.parentNode.nodeName.toLowerCase() == "div") {
            div.innerHTML = div.innerHTML.replace(/<h1/g,"<h2").replace(/<\/h1>/g,"</h2>");
          }
        }
        
        //Add class attribute to nested kbd elements
        for (i = 0; i < document.getElementsByTagName("kbd").length; i++) {
          var kbd = document.getElementsByTagName("kbd")[i];
          if(kbd.parentNode.nodeName.toLowerCase() == "kbd") {
            kbd.className = "key";
          }
        }
        
        //Add Table of contents and footer tags for HTML2PDF converter
        document.getElementsByTagName("body")[0].innerHTML = '<div id="footerContent"><pdf:pagenumber></div><h1 id="index" class="book-heading">Table of Contents</h1><div><pdf:toc /></div>' + document.getElementsByTagName("body")[0].innerHTML;
      }
Attachment Size
handbook_modified.pdf 561.22 KB

Comments

Do you still have an unanswered question? Please log in first to post your question.