Personal tools

Cookbook/PDF files

From HaskellWiki

< Cookbook(Difference between revisions)
Jump to: navigation, search
(Drawing text)
Current revision (14:17, 18 August 2009) (edit) (undo)
(Pages with different sizes: translate OO-slang)
 
(21 intermediate revisions not shown.)
Line 1: Line 1:
-
For the following recipes you need to install [http://hackage.haskell.org/cgi-bin/hackage-scripts/package/HPDF HPDF].
+
For the following recipes you need to install [http://hackage.haskell.org/cgi-bin/hackage-scripts/package/HPDF HPDF], a pure Haskell PDF generation library.
-
= Creating an empty PDF file =
+
== Creating an empty PDF file ==
[http://hackage.haskell.org/packages/archive/HPDF/latest/doc/html/Graphics-PDF.html#v%3ArunPdf runPdf] generates a PDF file in the file system. You need to pass it four things:
[http://hackage.haskell.org/packages/archive/HPDF/latest/doc/html/Graphics-PDF.html#v%3ArunPdf runPdf] generates a PDF file in the file system. You need to pass it four things:
-
* a file name for the PDF file
+
* a name for the PDF file
-
* document information, an instance of [http://hackage.haskell.org/packages/archive/HPDF/latest/doc/html/Graphics-PDF-Document.html#t%3APDFDocumentInfo PDFDocumentInfo]. You can use the data generated by [http://hackage.haskell.org/packages/archive/HPDF/latest/doc/html/Graphics-PDF-Document.html#v%3AstandardDocInfo standardDocInfo].
+
* document information, of type [http://hackage.haskell.org/packages/archive/HPDF/latest/doc/html/Graphics-PDF-Document.html#t%3APDFDocumentInfo PDFDocumentInfo]. You can use the data generated by [http://hackage.haskell.org/packages/archive/HPDF/latest/doc/html/Graphics-PDF-Document.html#v%3AstandardDocInfo standardDocInfo].
-
* the default page size, an instance of [http://hackage.haskell.org/packages/archive/HPDF/latest/doc/html/Graphics-PDF.html#t%3APDFRect PDFRect].
+
* the default page size, of type [http://hackage.haskell.org/packages/archive/HPDF/latest/doc/html/Graphics-PDF.html#t%3APDFRect PDFRect].
* a [http://hackage.haskell.org/packages/archive/HPDF/latest/doc/html/Graphics-PDF.html#t%3APDF PDF Action]
* a [http://hackage.haskell.org/packages/archive/HPDF/latest/doc/html/Graphics-PDF.html#t%3APDF PDF Action]
Line 18: Line 18:
main :: IO ()
main :: IO ()
main = do
main = do
-
let outputFileName= "test1.pdf"
+
let pdfFileName= "test1.pdf"
let documentInfo = standardDocInfo
let documentInfo = standardDocInfo
let defaultPageSize = PDFRect 0 0 200 300
let defaultPageSize = PDFRect 0 0 200 300
-
runPdf outputFileName documentInfo defaultPageSize $ do
+
runPdf pdfFileName documentInfo defaultPageSize $ do
addPage Nothing
addPage Nothing
</haskell>
</haskell>
-
= Pages with different sizes =
+
== Pages with different sizes ==
If you pass "Nothing" to [http://hackage.haskell.org/packages/archive/HPDF/latest/doc/html/Graphics-PDF-Document.html#v%3AaddPage addPage], the default page size will be used for the size of the new page.
If you pass "Nothing" to [http://hackage.haskell.org/packages/archive/HPDF/latest/doc/html/Graphics-PDF-Document.html#v%3AaddPage 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 [http://hackage.haskell.org/packages/archive/HPDF/latest/doc/html/Graphics-PDF.html#t%3APDFRect PDFRect]).
+
We can pass it a different size (of type [http://hackage.haskell.org/packages/archive/HPDF/latest/doc/html/Graphics-PDF.html#t%3APDFRect PDFRect]).
Let’s create three pages, the last two pages with different dimensions:
Let’s create three pages, the last two pages with different dimensions:
Line 38: Line 38:
main :: IO ()
main :: IO ()
main = do
main = do
-
let outputFileName= "test2.pdf"
+
let pdfFileName = "test2.pdf"
let documentInfo = standardDocInfo
let documentInfo = standardDocInfo
let defaultPageSize = PDFRect 0 0 200 300
let defaultPageSize = PDFRect 0 0 200 300
-
runPdf outputFileName documentInfo defaultPageSize $ do
+
runPdf pdfFileName documentInfo defaultPageSize $ do
addPage Nothing
addPage Nothing
addPage $ Just $ PDFRect 0 0 100 100
addPage $ Just $ PDFRect 0 0 100 100
Line 48: Line 48:
</haskell>
</haskell>
-
= Drawing text =
+
== Drawing a simple text ==
-
TODO
+
To draw a simple text you need to take the following steps:
-
* [http://hackage.haskell.org/packages/archive/HPDF/latest/doc/html/Graphics-PDF-Text.html#v%3AtoPDFString toPDFString]
+
* convert the text to a [http://hackage.haskell.org/packages/archive/HPDF/latest/doc/html/Graphics-PDF.html#t%3APDFString PDFString] using [http://hackage.haskell.org/packages/archive/HPDF/latest/doc/html/Graphics-PDF-Text.html#v%3AtoPDFString toPDFString]
-
* [http://hackage.haskell.org/packages/archive/HPDF/latest/doc/html/Graphics-PDF-Text.html#t%3APDFFont PDFFont]
+
* create a [http://hackage.haskell.org/packages/archive/HPDF/latest/doc/html/Graphics-PDF-Text.html#t%3APDFFont PDFFont], passing it
-
** [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]
-
* [http://hackage.haskell.org/packages/archive/HPDF/latest/doc/html/Graphics-PDF-Text.html#v%3Atext text]
+
** a font size
-
* [http://hackage.haskell.org/packages/archive/HPDF/latest/doc/html/Graphics-PDF-Text.html#v%3AdrawText drawText]
+
* 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], passing it
 +
** the PDFFont from above
 +
** a x,y-coordinate
 +
** the PDFString 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
 +
 
 +
Example:
 +
<haskell>
 +
import Graphics.PDF
 +
 +
main :: IO ()
 +
main = do
 +
let pdfFileName = "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
 +
 +
runPdf pdfFileName documentInfo defaultPageSize $ do
 +
page <- addPage Nothing
 +
 
 +
drawWithPage page $ do
 +
drawText pdfText
 +
</haskell>

Current revision

For the following recipes you need to install HPDF, a pure Haskell PDF generation library.

1 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 pdfFileName= "test1.pdf"
  let documentInfo = standardDocInfo 
  let defaultPageSize = PDFRect 0 0 200 300
 
  runPdf pdfFileName documentInfo defaultPageSize $ do
    addPage Nothing

2 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 (of type PDFRect).

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

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

3 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 pdfFileName = "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
 
  runPdf pdfFileName documentInfo defaultPageSize $ do
    page <- addPage Nothing
 
    drawWithPage page $ do
      drawText pdfText