Difference between revisions of "Cookbook/PDF files"

From HaskellWiki
Jump to navigation Jump to search
Line 56: Line 56:
 
** a [http://hackage.haskell.org/packages/archive/HPDF/latest/doc/html/Graphics-PDF-Text.html#t%3AFontName FontName]
 
** a [http://hackage.haskell.org/packages/archive/HPDF/latest/doc/html/Graphics-PDF-Text.html#t%3AFontName FontName]
 
** a font size
 
** a font size
* create a [http://hackage.haskell.org/packages/archive/HPDF/latest/doc/html/Graphics-PDF-Text.html#t%3APDFText PDFText] using [http://hackage.haskell.org/packages/archive/HPDF/latest/doc/html/Graphics-PDF-Text.html#v%3Atext text] and the data above
+
* create a [http://hackage.haskell.org/packages/archive/HPDF/latest/doc/html/Graphics-PDF-Text.html#t%3APDFText PDFText] using [http://hackage.haskell.org/packages/archive/HPDF/latest/doc/html/Graphics-PDF-Text.html#v%3Atext text] and the data above and a x,y-coordinate
 
* create a [http://hackage.haskell.org/packages/archive/HPDF/latest/doc/html/Graphics-PDF-Document.html#t%3ADraw Draw action] using [http://hackage.haskell.org/packages/archive/HPDF/latest/doc/html/Graphics-PDF-Text.html#v%3AdrawText drawText] and the PDFText from above
 
* create a [http://hackage.haskell.org/packages/archive/HPDF/latest/doc/html/Graphics-PDF-Document.html#t%3ADraw Draw action] using [http://hackage.haskell.org/packages/archive/HPDF/latest/doc/html/Graphics-PDF-Text.html#v%3AdrawText drawText] and the PDFText from above
 
* draw on a given page using [http://hackage.haskell.org/packages/archive/HPDF/latest/doc/html/Graphics-PDF-Document.html#v%3AdrawWithPage drawWithPage] and the Draw action from above
 
* draw on a given page using [http://hackage.haskell.org/packages/archive/HPDF/latest/doc/html/Graphics-PDF-Document.html#v%3AdrawWithPage drawWithPage] and the Draw action from above

Revision as of 08:49, 24 April 2009

For the following recipes you need to install HPDF.

Creating an empty PDF file

runPdf generates a PDF file in the file system. You need to pass it four things:

Let's create an empty PDF file with the name "test1.pdf". We use addPage to add an empty page.

import Graphics.PDF

main :: IO ()
main = do
  let outputFileName= "test1.pdf"
  let documentInfo = standardDocInfo 
  let defaultPageSize = PDFRect 0 0 200 300
  
  runPdf outputFileName documentInfo defaultPageSize $ do
    addPage Nothing

Pages with different sizes

If you pass "Nothing" to addPage, the default page size will be used for the size of the new page. We can pass it a different size (an instance of PDFRect).

Let’s create three pages, the last two pages with different dimensions:

import Graphics.PDF

main :: IO ()
main = do
  let outputFileName= "test2.pdf"
  let documentInfo = standardDocInfo 
  let defaultPageSize = PDFRect 0 0 200 300
  
  runPdf outputFileName documentInfo defaultPageSize $ do
    addPage Nothing
    addPage $ Just $ PDFRect 0 0 100 100
    addPage $ Just $ PDFRect 0 0 150 150

Drawing a simple text

To draw a simple text you need to take the following steps:

Example:

import Graphics.PDF
 
main :: IO ()
main = do
  let outputFileName= "test3.pdf"
  let documentInfo = standardDocInfo 
  let defaultPageSize = PDFRect 0 0 200 300

  let pdfString = toPDFString "Hello World!"
  let pdfFont = PDFFont Times_Roman 32
  let pdfText = text pdfFont 10.0 10.0 pdfString
  let drawAction = drawText pdfText
  
  runPdf outputFileName documentInfo defaultPageSize $ do
    page <- addPage Nothing
    drawWithPage page drawAction