$30 off During Our Annual Pro Sale. View Details »

Automatic Printing System with Open Source Tools

Mosky Liu
August 03, 2013

Automatic Printing System with Open Source Tools

It is the slides for COSCUP[1] 2013 talk, "Automatic Printing System with Open Source Tools".

It explains how to setup automatic printing system on Linux and includes some tips for it.

[1] COSCUP: http://coscup.org/

Mosky Liu

August 03, 2013
Tweet

More Decks by Mosky Liu

Other Decks in Programming

Transcript

  1. 2013/08/03 @ COSCUP 2013
    Automatic Printing System
    with Open Source Tools
    Mosky
    1

    View Slide

  2. This Slide
    ✤ This slide is available at:
    https://speakerdeck.com/mosky/automatic-printing-system-with-
    open-source-tools
    2

    View Slide

  3. Mosky
    3

    View Slide

  4. Mosky
    ✤ I am working at www.pinkoi.com .
    3

    View Slide

  5. Mosky
    ✤ I am working at www.pinkoi.com .
    ✤ I've been using Ubuntu everyday since 2009.
    3

    View Slide

  6. Mosky
    ✤ I am working at www.pinkoi.com .
    ✤ I've been using Ubuntu everyday since 2009.
    ✤ A speaker at PyCon TW 2013, 2012, PyCon JP 2012, ...
    3

    View Slide

  7. Mosky
    ✤ I am working at www.pinkoi.com .
    ✤ I've been using Ubuntu everyday since 2009.
    ✤ A speaker at PyCon TW 2013, 2012, PyCon JP 2012, ...
    ✤ http://mosky.tw/
    3

    View Slide

  8. Outline
    4

    View Slide

  9. Outline
    ✤ The Motivation
    4

    View Slide

  10. Outline
    ✤ The Motivation
    ✤ Setup Printing System on Linux
    4

    View Slide

  11. Outline
    ✤ The Motivation
    ✤ Setup Printing System on Linux
    ✤ Convert Web to PDF
    4

    View Slide

  12. Outline
    ✤ The Motivation
    ✤ Setup Printing System on Linux
    ✤ Convert Web to PDF
    ✤ The Tips
    4

    View Slide

  13. The Motivation
    5

    View Slide

  14. It's just out of control!
    We have to issue more than 1,000 invoices per month.
    6

    View Slide

  15. It's just out of control!
    We have to issue more than 1,000 invoices per month.
    6

    View Slide

  16. We love hand-made stuff.
    Pinkoi is the largest online community and marketplace for designers in Asia
    7

    View Slide

  17. We love hand-made stuff.
    Pinkoi is the largest online community and marketplace for designers in Asia
    7

    View Slide

  18. So we build it for ourselves
    with many cups of coffee.
    8

    View Slide

  19. So we build it for ourselves
    with many cups of coffee.
    8

    View Slide

  20. Setup Printing System on Linux
    9

    View Slide

  21. The Check List
    10

    View Slide

  22. The Check List
    ✤ A computer installed Linux and CUPS
    (Common Unix Printing System).
    10

    View Slide

  23. The Check List
    ✤ A computer installed Linux and CUPS
    (Common Unix Printing System).
    ✤ http://www.cups.org/
    10

    View Slide

  24. The Check List
    ✤ A computer installed Linux and CUPS
    (Common Unix Printing System).
    ✤ http://www.cups.org/
    ✤ http://localhost:631/
    10

    View Slide

  25. The Check List
    ✤ A computer installed Linux and CUPS
    (Common Unix Printing System).
    ✤ http://www.cups.org/
    ✤ http://localhost:631/
    ✤ A printer which has an IP address.
    10

    View Slide

  26. The Check List
    ✤ A computer installed Linux and CUPS
    (Common Unix Printing System).
    ✤ http://www.cups.org/
    ✤ http://localhost:631/
    ✤ A printer which has an IP address.
    ✤ http://192.168.2.100/
    10

    View Slide

  27. The Check List
    ✤ A computer installed Linux and CUPS
    (Common Unix Printing System).
    ✤ http://www.cups.org/
    ✤ http://localhost:631/
    ✤ A printer which has an IP address.
    ✤ http://192.168.2.100/
    ✤ lpd://192.168.2.100/
    10

    View Slide

  28. Setup Printer with CUPS' Driver
    11

    View Slide

  29. Setup Printer with CUPS' Driver
    ✤ Find the driver for your printer:
    11

    View Slide

  30. Setup Printer with CUPS' Driver
    ✤ Find the driver for your printer:
    ✤ $ lpinfo --make-and-model 'P1006' -m
    11

    View Slide

  31. Setup Printer with CUPS' Driver
    ✤ Find the driver for your printer:
    ✤ $ lpinfo --make-and-model 'P1006' -m
    ✤ drv:///hpijs.drv/hp-laserjet_p1006-hpijs.ppd HP LaserJet
    p1006 hpijs, 3.10.2, requires proprietary plugin
    11

    View Slide

  32. Setup Printer with CUPS' Driver
    ✤ Find the driver for your printer:
    ✤ $ lpinfo --make-and-model 'P1006' -m
    ✤ drv:///hpijs.drv/hp-laserjet_p1006-hpijs.ppd HP LaserJet
    p1006 hpijs, 3.10.2, requires proprietary plugin
    ✤ Add the printer with the driver we found from the above command:
    11

    View Slide

  33. Setup Printer with CUPS' Driver
    ✤ Find the driver for your printer:
    ✤ $ lpinfo --make-and-model 'P1006' -m
    ✤ drv:///hpijs.drv/hp-laserjet_p1006-hpijs.ppd HP LaserJet
    p1006 hpijs, 3.10.2, requires proprietary plugin
    ✤ Add the printer with the driver we found from the above command:
    ✤ $ lpadmin -E -p -v socket:// -m -E
    11

    View Slide

  34. Setup Printer with CUPS' Driver
    ✤ Find the driver for your printer:
    ✤ $ lpinfo --make-and-model 'P1006' -m
    ✤ drv:///hpijs.drv/hp-laserjet_p1006-hpijs.ppd HP LaserJet
    p1006 hpijs, 3.10.2, requires proprietary plugin
    ✤ Add the printer with the driver we found from the above command:
    ✤ $ lpadmin -E -p -v socket:// -m -E
    ✤ $ lpadmin -E -p 'hp-p1006' -v socket://192.168.2.100 -m
    drv:///hpijs.drv/hp-laserjet_p1006-hpijs.ppd -E
    11

    View Slide

  35. Setup Printer with PPD
    12

    View Slide

  36. Setup Printer with PPD
    ✤ Find the PPD file for your printer:
    12

    View Slide

  37. Setup Printer with PPD
    ✤ Find the PPD file for your printer:
    ✤ http://www.openprinting.org/drivers
    12

    View Slide

  38. Setup Printer with PPD
    ✤ Find the PPD file for your printer:
    ✤ http://www.openprinting.org/drivers
    ✤ Put the PPD file in:
    12

    View Slide

  39. Setup Printer with PPD
    ✤ Find the PPD file for your printer:
    ✤ http://www.openprinting.org/drivers
    ✤ Put the PPD file in:
    ✤ /usr/share/cups/model
    12

    View Slide

  40. Setup Printer with PPD
    ✤ Find the PPD file for your printer:
    ✤ http://www.openprinting.org/drivers
    ✤ Put the PPD file in:
    ✤ /usr/share/cups/model
    ✤ Add the printer with this PPD file:
    12

    View Slide

  41. Setup Printer with PPD
    ✤ Find the PPD file for your printer:
    ✤ http://www.openprinting.org/drivers
    ✤ Put the PPD file in:
    ✤ /usr/share/cups/model
    ✤ Add the printer with this PPD file:
    ✤ $ lpadmin -E -p -v lpd:///lp -m -E
    12

    View Slide

  42. Setup Printer with PPD
    ✤ Find the PPD file for your printer:
    ✤ http://www.openprinting.org/drivers
    ✤ Put the PPD file in:
    ✤ /usr/share/cups/model
    ✤ Add the printer with this PPD file:
    ✤ $ lpadmin -E -p -v lpd:///lp -m -E
    ✤ $ lpadmin -E -p 'hp-p1006' -v lpd://192.168.2.100/lp -m
    12

    View Slide

  43. More about lpadmin
    13

    View Slide

  44. More about lpadmin
    ✤ Enable a printer:
    13

    View Slide

  45. More about lpadmin
    ✤ Enable a printer:
    ✤ $ lpadmin -p -E
    13

    View Slide

  46. More about lpadmin
    ✤ Enable a printer:
    ✤ $ lpadmin -p -E
    ✤ Set a printer as default:
    13

    View Slide

  47. More about lpadmin
    ✤ Enable a printer:
    ✤ $ lpadmin -p -E
    ✤ Set a printer as default:
    ✤ $ lpadmin -d
    13

    View Slide

  48. More about lpadmin
    ✤ Enable a printer:
    ✤ $ lpadmin -p -E
    ✤ Set a printer as default:
    ✤ $ lpadmin -d
    ✤ Remove a printer:
    13

    View Slide

  49. More about lpadmin
    ✤ Enable a printer:
    ✤ $ lpadmin -p -E
    ✤ Set a printer as default:
    ✤ $ lpadmin -d
    ✤ Remove a printer:
    ✤ $ lpadmin -x
    13

    View Slide

  50. Check Printer is Online
    14

    View Slide

  51. Check Printer is Online
    ✤ Just print a test file:
    14

    View Slide

  52. Check Printer is Online
    ✤ Just print a test file:
    ✤ $ lpr -P
    14

    View Slide

  53. Check Printer is Online
    ✤ Just print a test file:
    ✤ $ lpr -P
    ✤ Check the status of printer:
    14

    View Slide

  54. Check Printer is Online
    ✤ Just print a test file:
    ✤ $ lpr -P
    ✤ Check the status of printer:
    ✤ $ lpstat -a / $ lpstat -t
    14

    View Slide

  55. Check Printer is Online
    ✤ Just print a test file:
    ✤ $ lpr -P
    ✤ Check the status of printer:
    ✤ $ lpstat -a / $ lpstat -t
    ✤ Troubleshooting:
    14

    View Slide

  56. Check Printer is Online
    ✤ Just print a test file:
    ✤ $ lpr -P
    ✤ Check the status of printer:
    ✤ $ lpstat -a / $ lpstat -t
    ✤ Troubleshooting:
    ✤ $ nc -v
    14

    View Slide

  57. Check Printer is Online
    ✤ Just print a test file:
    ✤ $ lpr -P
    ✤ Check the status of printer:
    ✤ $ lpstat -a / $ lpstat -t
    ✤ Troubleshooting:
    ✤ $ nc -v
    ✤ $ nmap -p
    14

    View Slide

  58. Convert Web to PDF
    15

    View Slide

  59. The Packages
    16

    View Slide

  60. The Packages
    ✤ Use wkhtmltopdf to convert Web to PDF.
    16

    View Slide

  61. The Packages
    ✤ Use wkhtmltopdf to convert Web to PDF.
    ✤ $ sudo apt-get install wkhtmltopdf
    16

    View Slide

  62. The Packages
    ✤ Use wkhtmltopdf to convert Web to PDF.
    ✤ $ sudo apt-get install wkhtmltopdf
    ✤ https://code.google.com/p/wkhtmltopdf/
    16

    View Slide

  63. The Packages
    ✤ Use wkhtmltopdf to convert Web to PDF.
    ✤ $ sudo apt-get install wkhtmltopdf
    ✤ https://code.google.com/p/wkhtmltopdf/
    ✤ If you don't have X Window, you also need xvfb.
    16

    View Slide

  64. The Packages
    ✤ Use wkhtmltopdf to convert Web to PDF.
    ✤ $ sudo apt-get install wkhtmltopdf
    ✤ https://code.google.com/p/wkhtmltopdf/
    ✤ If you don't have X Window, you also need xvfb.
    ✤ $ sudo apt-get install xvfb
    16

    View Slide

  65. The Usage
    17

    View Slide

  66. The Usage
    ✤ with X Window:
    17

    View Slide

  67. The Usage
    ✤ with X Window:
    ✤ $ wkhtmltopdf
    17

    View Slide

  68. The Usage
    ✤ with X Window:
    ✤ $ wkhtmltopdf
    ✤ $ wkhtmltopdf -T 0 -L 0 -R 0 -B 0 --page-size A4 --
    encoding utf-8
    17

    View Slide

  69. The Usage
    ✤ with X Window:
    ✤ $ wkhtmltopdf
    ✤ $ wkhtmltopdf -T 0 -L 0 -R 0 -B 0 --page-size A4 --
    encoding utf-8
    ✤ without X window:
    17

    View Slide

  70. The Usage
    ✤ with X Window:
    ✤ $ wkhtmltopdf
    ✤ $ wkhtmltopdf -T 0 -L 0 -R 0 -B 0 --page-size A4 --
    encoding utf-8
    ✤ without X window:
    ✤ $ xvfb-run -a -s "-screen 0 640x480x16" wkhtmltopdf -T 0
    -L 0 -R 0 -B 0 --page-size A4 --encoding utf-8

    17

    View Slide

  71. Send File to Printer
    18

    View Slide

  72. Send File to Printer
    ✤ Print file:
    18

    View Slide

  73. Send File to Printer
    ✤ Print file:
    ✤ $ lpr -P ...
    18

    View Slide

  74. Send File to Printer
    ✤ Print file:
    ✤ $ lpr -P ...
    ✤ Show printer queue:
    18

    View Slide

  75. Send File to Printer
    ✤ Print file:
    ✤ $ lpr -P ...
    ✤ Show printer queue:
    ✤ $ lpq -P
    18

    View Slide

  76. Send File to Printer
    ✤ Print file:
    ✤ $ lpr -P ...
    ✤ Show printer queue:
    ✤ $ lpq -P
    ✤ $ lpq -a
    18

    View Slide

  77. Send File to Printer
    ✤ Print file:
    ✤ $ lpr -P ...
    ✤ Show printer queue:
    ✤ $ lpq -P
    ✤ $ lpq -a
    ✤ Cancel print job:
    18

    View Slide

  78. Send File to Printer
    ✤ Print file:
    ✤ $ lpr -P ...
    ✤ Show printer queue:
    ✤ $ lpq -P
    ✤ $ lpq -a
    ✤ Cancel print job:
    ✤ $ lprm -P ...
    18

    View Slide

  79. PyCUPS
    19

    View Slide

  80. PyCUPS
    ✤ PyPI
    19

    View Slide

  81. PyCUPS
    ✤ PyPI
    ✤ https://pypi.python.org/pypi/pycups
    19

    View Slide

  82. PyCUPS
    ✤ PyPI
    ✤ https://pypi.python.org/pypi/pycups
    ✤ Get connection:
    19

    View Slide

  83. PyCUPS
    ✤ PyPI
    ✤ https://pypi.python.org/pypi/pycups
    ✤ Get connection:
    ✤ import cups
    conn = cups.Connection()
    19

    View Slide

  84. PyCUPS - Get Printers
    20

    View Slide

  85. PyCUPS - Get Printers
    ✤ Get printers:
    20

    View Slide

  86. PyCUPS - Get Printers
    ✤ Get printers:
    ✤ conn.getPrinters()
    20

    View Slide

  87. PyCUPS - Get Printers
    ✤ Get printers:
    ✤ conn.getPrinters()
    ✤ Example output:
    20

    View Slide

  88. PyCUPS - Get Printers
    ✤ Get printers:
    ✤ conn.getPrinters()
    ✤ Example output:
    ✤ {u'hp': {'device-uri': u'lpd://192.168.0.100/lp',
    'printer-info': u'hp',
    # 3: prepared to print; 4: printing; 5: this device is stop
    'printer-state': 3,
    'printer-state-message': u'Data file sent successfully',
    'printer-state-reasons': [u'none'],
    ...
    'printer-uri-supported': u'ipp://localhost:631/printers/hp'}}
    20

    View Slide

  89. PyCUPS - Print File
    21

    View Slide

  90. PyCUPS - Print File
    ✤ Print file:
    21

    View Slide

  91. PyCUPS - Print File
    ✤ Print file:
    ✤ conn.printFile(printer, filename, title, options)
    conn.printFiles(printer, filenames, title, options)
    21

    View Slide

  92. PyCUPS - Print File
    ✤ Print file:
    ✤ conn.printFile(printer, filename, title, options)
    conn.printFiles(printer, filenames, title, options)
    ✤ Example:
    21

    View Slide

  93. PyCUPS - Print File
    ✤ Print file:
    ✤ conn.printFile(printer, filename, title, options)
    conn.printFiles(printer, filenames, title, options)
    ✤ Example:
    ✤ conn.printFile(printer, filepath,
    os.path.basename(filepath), {})
    21

    View Slide

  94. PyCUPS - Get Jobs
    22

    View Slide

  95. PyCUPS - Get Jobs
    ✤ Get jobs:
    22

    View Slide

  96. PyCUPS - Get Jobs
    ✤ Get jobs:
    ✤ conn.getJobs(which_jobs='not-completed', my_jobs=False,
    limit=-1, first_job_id=-1, requested_attributes=None)
    22

    View Slide

  97. PyCUPS - Get Jobs
    ✤ Get jobs:
    ✤ conn.getJobs(which_jobs='not-completed', my_jobs=False,
    limit=-1, first_job_id=-1, requested_attributes=None)
    ✤ The which_jobs can be 'not-completed', 'completed' or 'all'.
    22

    View Slide

  98. PyCUPS - Get Jobs
    ✤ Get jobs:
    ✤ conn.getJobs(which_jobs='not-completed', my_jobs=False,
    limit=-1, first_job_id=-1, requested_attributes=None)
    ✤ The which_jobs can be 'not-completed', 'completed' or 'all'.
    ✤ It returns a dict, indexed by job id, of dicts representing job attributes.
    22

    View Slide

  99. The Tips
    23

    View Slide

  100. The Error Log
    24

    View Slide

  101. The Error Log
    ✤ $ sudo tail -f /var/log/cups/error_log
    24

    View Slide

  102. About Template (CSS)
    25

    View Slide

  103. About Template (CSS)
    ✤ Use cm/mm as your unit ...
    25

    View Slide

  104. About Template (CSS)
    ✤ Use cm/mm as your unit ...
    ✤ The steps of adjusting output:
    25

    View Slide

  105. About Template (CSS)
    ✤ Use cm/mm as your unit ...
    ✤ The steps of adjusting output:
    ✤ Create 1x1 cm div block.
    25

    View Slide

  106. About Template (CSS)
    ✤ Use cm/mm as your unit ...
    ✤ The steps of adjusting output:
    ✤ Create 1x1 cm div block.
    ✤ Print it.
    25

    View Slide

  107. About Template (CSS)
    ✤ Use cm/mm as your unit ...
    ✤ The steps of adjusting output:
    ✤ Create 1x1 cm div block.
    ✤ Print it.
    ✤ Use zoom to adjust.
    25

    View Slide

  108. About Template (CSS)
    ✤ Use cm/mm as your unit ...
    ✤ The steps of adjusting output:
    ✤ Create 1x1 cm div block.
    ✤ Print it.
    ✤ Use zoom to adjust.
    ✤ Avoid floating structure.
    25

    View Slide

  109. Live Demo
    26

    View Slide

  110. Live Demo
    27

    View Slide

  111. Video Demo
    28

    View Slide

  112. 29

    View Slide

  113. 29

    View Slide

  114. The End
    30

    View Slide

  115. The End
    31

    View Slide

  116. The End
    ✤ CUPS
    31

    View Slide

  117. The End
    ✤ CUPS
    ✤ lpadmin, lpstat, lpr, lpq, lprm, ...
    31

    View Slide

  118. The End
    ✤ CUPS
    ✤ lpadmin, lpstat, lpr, lpq, lprm, ...
    ✤ wxhtmltopdf
    31

    View Slide

  119. The End
    ✤ CUPS
    ✤ lpadmin, lpstat, lpr, lpq, lprm, ...
    ✤ wxhtmltopdf
    ✤ PyCUPS
    31

    View Slide

  120. The End
    ✤ CUPS
    ✤ lpadmin, lpstat, lpr, lpq, lprm, ...
    ✤ wxhtmltopdf
    ✤ PyCUPS
    ✤ Some tips
    31

    View Slide

  121. The End
    ✤ CUPS
    ✤ lpadmin, lpstat, lpr, lpq, lprm, ...
    ✤ wxhtmltopdf
    ✤ PyCUPS
    ✤ Some tips
    ✤ Any Question?
    31

    View Slide