This repository was archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathapi.py
More file actions
178 lines (151 loc) · 4.89 KB
/
Copy pathapi.py
File metadata and controls
178 lines (151 loc) · 4.89 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#-*- coding: utf-8 -*-
import json
import requests
import datetime
import frappe
import hashlib
import subprocess
from base64 import b64encode, b64decode
from frappe import _
from frappe.utils import flt, cint, get_datetime, date_diff, nowdate, now_datetime
try:
from frappe.utils.file_manager import get_file
except ImportError:
from frappe.core.doctype.file.file import download_file
def get_file(file_url):
download_file(file_url)
file_content = frappe.response.file_content
del frappe.local.response.file_content
del frappe.local.response.file_name
del frappe.local.response.type
return [file_url, file_content]
from frappe.utils.jinja import render_template
from frappe.utils.data import scrub_urls
from frappe.utils.pdf import get_pdf
from xmlescpos.escpos import Escpos, StyleStack
from pdfkit.pdfkit import PDFKit
from six import string_types
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
try:
from printnodeapi.Gateway import Gateway
except ImportError:
from printnodeapi.gateway import Gateway
class IOPrinter(Escpos):
def __init__(self):
self.slip_sheet_mode = False
self.io = StringIO()
self.stylestack = StyleStack()
def _raw(self, msg):
self.io.write(msg)
def get_content(self):
return self.io.getvalue()
class PDFKit(PDFKit):
def to_image(self, path):
try:
return self.to_pdf(path)
except UnicodeDecodeError, e:
pass
class Configuration(object):
pass
def get_print_content(print_format, doctype, docname, is_escpos=False, is_raw=False):
if is_escpos or is_raw:
doc = frappe.get_doc(doctype, docname)
template = frappe.db.get_value("Print Format", print_format, "html")
content = render_template(template, {"doc": doc})
if is_escpos:
content.replace("<br>", "<br/>")
else:
content = frappe.get_print(doctype, docname, print_format)
if is_escpos:
printer = IOPrinter()
printer.receipt(content)
raw = printer.get_content()
elif is_raw:
raw = content
else:
raw = get_pdf(content)
#frappe.msgprint("<pre>%s</pre>" %raw)
return b64encode(raw)
@frappe.whitelist()
def print_via_printnode(action, **kwargs):
settings = frappe.get_doc("Print Node Settings", "Print Node Settings")
if not settings.api_key:
frappe.throw(
_("Your Print Node API Key is not configured in Print Node Settings")
)
if not frappe.db.exists("Print Node Action", action):
frappe.throw(
_("Unable to find an action in Print settings to execute this print")
)
else:
action = frappe.get_doc("Print Node Action", action)
if not kwargs.get('doctype') and action.get('print_format'):
kwargs['doctype'] = frappe.db.get_value('Print Format', action.print_format, 'doc_type')
if action.get("capabilities"):
print_settings = json.loads(action.capabilities)
else:
print_settings = {}
if 'collate' in print_settings:
print_settings['collate'] = bool(print_settings['collate'])
printer = frappe.db.get_value("Print Node Hardware", action.printer, "hw_id")
gateway = Gateway(apikey=settings.api_key)
if action.printable_type == "Print Format":
print_content = get_print_content(
action.print_format if not action.use_standard else "Standard",
kwargs.get("doctype"),
kwargs.get("docname"),
action.is_xml_esc_pos,
action.is_raw_text
)
raw = action.is_xml_esc_pos or action.is_raw_text
gateway.PrintJob(
printer=int(printer),
job_type="raw" if raw else "pdf",
title=action.action,
base64=print_content,
options=print_settings
)
else:
print_content = b64encode(get_file(kwargs.get("filename"))[1])
gateway.PrintJob(
printer=int(printer),
job_type="pdf" if kwargs.get("filename", "").lower().endswith(".pdf") else "raw",
base64=print_content,
options=print_settings
)
job = frappe.new_doc("Print Node Job").update({
"print_node_action": action.name,
"printer_id": action.printer,
"print_type": "File" if action.printable_type == "Attachment" else "Print Format",
"file_link": kwargs.get("filename"),
"print_format": action.print_format if not action.use_standard else "Standard",
"ref_type": kwargs.get("doctype"),
"ref_name": kwargs.get("docname"),
"is_xml_esc_pos": action.is_xml_esc_pos,
"is_raw_text": action.is_raw_text,
"print_job_name": action.action,
"copies": print_settings.get('copies', 1),
"job_owner": frappe.local.session.user,
"print_timestamp": now_datetime()
})
job.flags.ignore_permissions = True
job.flags.ignore_links = True
job.flags.ignore_validate = True
job.insert()
@frappe.whitelist()
def batch_print_via_printnode(action, docs):
if isinstance(docs, string_types):
docs = json.loads(docs)
for doc in docs:
print_via_printnode(action, **doc)
@frappe.whitelist()
def get_action_list(dt):
return frappe.get_all('Print Node Action',
fields=["name", "action", "printable_type", "attachment_pattern", "depends_on", "allow_inline_batch", "batch_field", "hotkey"],
filters={'dt': dt},
order_by= 'idx ASC',
limit_page_length=50
)