Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Mosky 3

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

Outline 4

Slide 9

Slide 9 text

Outline ✤ The Motivation 4

Slide 10

Slide 10 text

Outline ✤ The Motivation ✤ Setup Printing System on Linux 4

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

The Motivation 5

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

Setup Printing System on Linux 9

Slide 21

Slide 21 text

The Check List 10

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

Setup Printer with CUPS' Driver 11

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

Setup Printer with PPD 12

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

More about lpadmin 13

Slide 44

Slide 44 text

More about lpadmin ✤ Enable a printer: 13

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

Check Printer is Online 14

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

Convert Web to PDF 15

Slide 59

Slide 59 text

The Packages 16

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

The Usage 17

Slide 66

Slide 66 text

The Usage ✤ with X Window: 17

Slide 67

Slide 67 text

The Usage ✤ with X Window: ✤ $ wkhtmltopdf 17

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

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

Slide 71

Slide 71 text

Send File to Printer 18

Slide 72

Slide 72 text

Send File to Printer ✤ Print file: 18

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

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

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

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

Slide 78

Slide 78 text

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

Slide 79

Slide 79 text

PyCUPS 19

Slide 80

Slide 80 text

PyCUPS ✤ PyPI 19

Slide 81

Slide 81 text

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

Slide 82

Slide 82 text

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

Slide 83

Slide 83 text

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

Slide 84

Slide 84 text

PyCUPS - Get Printers 20

Slide 85

Slide 85 text

PyCUPS - Get Printers ✤ Get printers: 20

Slide 86

Slide 86 text

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

Slide 87

Slide 87 text

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

Slide 88

Slide 88 text

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

Slide 89

Slide 89 text

PyCUPS - Print File 21

Slide 90

Slide 90 text

PyCUPS - Print File ✤ Print file: 21

Slide 91

Slide 91 text

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

Slide 92

Slide 92 text

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

Slide 93

Slide 93 text

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

Slide 94

Slide 94 text

PyCUPS - Get Jobs 22

Slide 95

Slide 95 text

PyCUPS - Get Jobs ✤ Get jobs: 22

Slide 96

Slide 96 text

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

Slide 97

Slide 97 text

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

Slide 98

Slide 98 text

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

Slide 99

Slide 99 text

The Tips 23

Slide 100

Slide 100 text

The Error Log 24

Slide 101

Slide 101 text

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

Slide 102

Slide 102 text

About Template (CSS) 25

Slide 103

Slide 103 text

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

Slide 104

Slide 104 text

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

Slide 105

Slide 105 text

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

Slide 106

Slide 106 text

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

Slide 107

Slide 107 text

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

Slide 108

Slide 108 text

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

Slide 109

Slide 109 text

Live Demo 26

Slide 110

Slide 110 text

Live Demo 27

Slide 111

Slide 111 text

Video Demo 28

Slide 112

Slide 112 text

29

Slide 113

Slide 113 text

29

Slide 114

Slide 114 text

The End 30

Slide 115

Slide 115 text

The End 31

Slide 116

Slide 116 text

The End ✤ CUPS 31

Slide 117

Slide 117 text

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

Slide 118

Slide 118 text

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

Slide 119

Slide 119 text

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

Slide 120

Slide 120 text

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

Slide 121

Slide 121 text

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