forked from hmazter/raspberrypi-label-printer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrintLabel.cpp
83 lines (69 loc) · 2.33 KB
/
PrintLabel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <cups/cups.h>
#include <cups/ppd.h>
#include <string>
#include <stdio.h>
#include <map>
#include <exception>
using namespace std;
const char* PrinterName = "DYMO_LabelWriter_450";
class Error: public exception
{
public:
Error(const string& Message): exception(), Message_(Message) {}
virtual ~Error() throw() {}
virtual const char* what() const throw() { return Message_.c_str(); }
private:
string Message_;
};
map <string, string> gPaperNames;
typedef pair<string, string> str_pair;
void print_printers(){
int i;
cups_dest_t *dests, *dest;
int num_dests = cupsGetDests(&dests);
for (i = num_dests, dest = dests; i > 0; i --, dest ++)
{
if (dest->instance)
printf("%s/%s\n", dest->name, dest->instance);
else
puts(dest->name);
}
}
int main(int argc, char** argv)
{
try
{
if (argc < 3)
throw Error("Usage: PrintLabel <labelSize> <ImageName> [<ImageName> ...]");
cout << "Printers:" << endl;
print_printers();
int num_options = 0;
cups_option_t* options = NULL;
std::string label_size = argv[1];
if (label_size.compare("57x32mm") == 0) {
num_options = cupsAddOption("PageSize", "w162h90", num_options, &options); //11354 Multi-Purpose
} else if (label_size.compare("41x89mm") == 0) {
num_options = cupsAddOption("PageSize", "w118h252", num_options, &options); //11356 White Name Badge
} else if (label_size.compare("54x101mm") == 0) {
num_options = cupsAddOption("PageSize", "w154h286.2", num_options, &options); //99014 Shipping
} else {
throw Error("Label size is not a valid size. Must be WxHmm. Check source code to find supported sizes");
}
//num_options = cupsAddOption("scaling", "100", num_options, &options);
//num_options = cupsAddOption("orientation-requested", "4", num_options, &options);
//num_options = cupsAddOption("DymoHalftoning", "ErrorDiffusion", num_options, &options);
//num_options = cupsAddOption("DymoPrintQuality", "Graphics", num_options, &options);
for (int i=2; i<argc; i++) {
cupsPrintFile(PrinterName, argv[i], "Label", num_options, options);
}
cupsFreeOptions(num_options, options);
return 0;
}
catch(std::exception& e)
{
fprintf(stderr, "%s", e.what());
fprintf(stderr, "\n");
return 1;
}
}