Printer (API)

From ComputerCraft Wiki
Jump to: navigation, search
This API requires the use of a wrapped peripheral!
This API does not behave like a regular API, to use it you must first wrap the peripheral and call the methods on the wrapped object. For more information see this page.


The Printer API allows you to interact with printers. Using it you can print on paper to create Printed Page. It also allows to get the current amount of paper and ink inside of the printer.

All printer functions are to be used after wrapping the printer.

Grid disk.png  Printer (API)
Function Return values Description
printer.newPage() boolean success Starts a new page. Returns true if page got started, false if not.
printer.endPage() boolean success Ends the page and prints the page to the output tray. Returns true if page was ended, false if not.
printer.write(string text) nil Writes text to the paper, works the same way as term.write().
printer.setCursorPos(number x, number y) nil Sets the cursor position on the paper, works the same way as term.setCursorPos().
printer.getCursorPos() number x, number y Returns the coordinates of the cursor on the paper, works the same way as term.getCursorPos().
printer.getPageSize() number width, number height Returns the size of the paper, works the same way as term.getSize().
printer.setPageTitle(string title) nil Sets the title of the page.
printer.getPaperLevel() number paperAmount Returns the amount of paper available in the paper tray.
printer.getInkLevel() number inkAmount Returns the amount of ink in the ink slot.


Examples

Wrapping the printer

Before you try to print anything you first have to wrap the printer. Lets say our printer is on the left side of the computer, we would wrap it like this:

local printer = peripheral.wrap("left")

Using the printer

After wrapping the printer we can use all functions from the Printer API:

local printer = peripheral.wrap("left") -- Wrap the printer
local ready = printer.newPage()

if ready then
  printer.write("This is on a paper")
  printer.setCursorPos(1, 3)
  printer.write("Setting cursor position")
  
  printer.setPageTitle("My page")
  printer.endPage()
else
  error("Could not create a page. Is there any paper and ink in the printer?")
end

Checking if there is ink and paper

Before you create a page you first have to check if there is paper to print on and if there is ink to print with in the printer. You can do that by using printer.getPaperLevel and printer.getInkLevel methods:

local printer = peripheral.wrap("left")

if printer.getPaperLevel() == 0 then -- If there is no paper in the printer
  error("There is no paper in the printer!")
end

if printer.getInkLevel() == 0 then -- If there is no ink in the printer
  error("There is no ink in the printer!")
end

-- There is paper and ink in the printer, we can now print a page

if printer.newPage() then
  printer.write("Tasks:")
  
  printer.setCursorPos(1, 3)
  printer.write("* Check melon farm")
  
  printer.setCursorPos(1, 4)
  printer.write("* Get more ender pearls")
  
  printer.setPageTitle("Tasks")
  printer.endPage()
else
  error("Page could not be created.")
end

Notes

  • Setting the title of the page is optional.
  • It is possible to print on already printed page. This way you can print one part of the page in one color and the other part in another color.
  • printer.write does not wrap words like print or write does. It also changes "\n" to "?".