PDF Document structure

A PDF is a collection of objects - mostly just dictionary and stream objects.

These objects can be created separately, manipulated and then added to a document, or they can be simultaneously created and added to an existing document in a single step.

This documents details

  1. Different ways of creating the PDF objects
  2. Different ways of assigning an object into a document

Creating objects

Stream objects

A stream object is really just a collection of bytes that can represent drawing instructions, images or definitions.

minipdf uses stream objects to represent the graphical elements to be drawn on a page.

Stream objects can be created in 3 ways:

Using the ptag collection of functions:

ptag$rect(x = 20, y = 30, width = 100, height = 200)
#> q
#> /GS11 gs
#> 1 w
#> 0 0 0 rg
#> 0 0 0 RG
#> 20 30 100 200 re b
#> Q

Using the original PDFDict R6 class

PDFRect$new(x = 20, y = 30, width = 100, height = 200)
#> q
#> /GS11 gs
#> 1 w
#> 0 0 0 rg
#> 0 0 0 RG
#> 20 30 100 200 re b
#> Q

Use the rect() method on a PDFDocument object. This has the side effect of also adding the newly created dict to the document itself.

doc <- PDFDocument$new()
new_rect  <- doc$rect(x = 20, y = 30, width = 100, height = 200)
new_rect
#> q
#> /GS11 gs
#> 1 w
#> 0 0 0 rg
#> 0 0 0 RG
#> 20 30 100 200 re b
#> Q

Dictionary objects

A dictionary object is just a representation of what in R would be called a named list. They can be created in 3 ways:

Using the dict function

dict(Page = "[1 0 R]")
#> <<
#>     /Page [1 0 R]
#> >>

Using the original PDFDict R6 class

PDFDict$new(Type = "/Font")
#> <<
#>     /Type /Font
#> >>

Use the dict() method on a PDFDocument object. This has the side effect of also adding the newly created dict to the document itself.

doc <- PDFDocument$new()
new_dict  <- doc$dict(Greg = "/StopSign")
new_dict
#> <<
#>     /Greg /StopSign
#> >>

You can also create a nested dict object:

dict(Page = "[1 0 R]", This = dict(Nested = "cool"))
#> <<
#>     /Page [1 0 R]
#>     /This <<
#>         /Nested cool
#>     >>
#> >>

Adding objects to a PDF document

Creating a simple PDF (Document-focussed R6 method)

  • Initialise the document by calling PDFDocument$new()
  • Call appropriate methods on this object to add geometry
doc <- PDFDocument$new(width = 400, height = 100)

doc$text("#Rstats", x = 30, y = 20, fontsize = 100, fill = 'lightblue3')
doc$line(x1=20, y1=10, x2=380, y2=10, linewidth = 5, stroke = '#123456')

doc$save("figures/example0a.pdf")

Creating a simple PDF (Stream-focussed R6 methods)

  • Initialise the document by calling PDFDocument$new()
  • Create stream objects separately
  • Add objects to the document
doc <- PDFDocument$new(width = 400, height = 100)

the_text <- PDFText$new("#Rstats", x = 30, y = 20, fontsize = 100, fill = 'lightblue3')
the_line <- PDFLine$new(x1=20, y1=10, x2=380, y2=10, linewidth = 5, stroke = '#123456')

the_text$fill('darkgreen') # Adjust colour after creation

doc$append(the_text, the_line)

doc$save("figures/example0b.pdf")

Creating a simple PDF (Using the ptag list of helper function)

  • Create stream objects using the ptag list of helper functions
  • Add objects to the document
  • Initialise the document using pdf_doc with these elements
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Create individiaul R6 objects
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
the_text <- ptag$text("#Rstats", x = 30, y = 20, fontsize = 100, fill = 'lightblue3')
the_line <- ptag$line(x1=20, y1=10, x2=380, y2=10, linewidth = 5, stroke = '#123456')

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Adjust the colour of the text
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
the_text$fill('hotpink')

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Initialise a document
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
doc <- pdf_doc(width = 400, height = 100, the_text, the_line)

doc$save("figures/example0c.pdf")