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
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
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:
PDFDocument$new()
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")
PDFDocument$new()
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")
ptag
list of helper function)ptag
list of helper functionspdf_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")