Next / Previous / Contents / TCC Help System / NM Tech homepage

10.22. hist.cgi: buildPdf(): Render the report as a PDF file

# - - -   b u i l d P d f

def buildPdf(db, txny, circleList, cbcHist):
    '''Render the report in PDF format.

      [ (db is a pycbc.CBCDatabase instance) and
        (txny is an xnomo3.Txny instance) and
        (circleList is a list of pycbc.Circle instances) and
        (cbcHist is a lib.CbcHist instance) ->
          if PDF_CONVERTER is available ->
              sys.stdout  +:=  an HTTP response containing the
                  report requested by HistArgs() using db and
                  txny, in PDF format
          else -> raise lib.ScriptError ]

For the logic that builds the XSL-FO file and then uses fop to convert it to PDF, see Section 10.23, “hist.cgi: foPhase(): Create the PDF report”. Errors anywhere within that function will throw a lib.ScriptError that will be caught here.

    #-- 1
    # [ if HistArgs() is consistent with db and txny ->
    #     a temp file +:=  the report requested by HistArgs() using
    #           db and txny, in PDF format
    #     pdfName  :=  the name of that temp file
    #   else ->
    #     sys.stdout  +:=  an XHTML error page
    #     stop execution ]
        pdfName = foPhase(db, txny, circleList, cbcHist)
    except lib.ScriptError, x:

Now that the PDF is finished and living in the named file, we can return it directly as the HTTP response, with the appropriate header; see Section 10.4.2, “HTTP_PDF_HEADER. The file is copied in large chunks as a compromise between efficiency and storage usage; see Section 10.4.1, “PDF_CHUNK_SIZE”.

    #-- 2
    # [ if file (pdfName) can be opened for reading as binary ->
    #     pdfFile  :=  that file, so opened
    #   else ->
    #     sys.stdout  +:=  an XHTML error page
    #     return ]
    pdfFile = open(pdfName, "rb")

    #-- 3
    # [ sys.stdout  +:=  (HTTP header for PDF) + (contents of pdfFile)
    #   pdfFile  :=  closed and deleted ]
    chunk =
    while len(chunk) > 0:
        chunk =