Skip to content

Commit ee0edb5

Browse files
committed
added my automatic shopify printing code
1 parent e1e6a5f commit ee0edb5

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dist/
2+
node_modules/
3+
.idea/
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import base64
2+
import json
3+
import os
4+
import requests
5+
from fpdf import FPDF, XPos, YPos
6+
7+
8+
def handler(request):
9+
PRINTNODE_API_KEY = os.getenv('PRINTNODE_API_KEY')
10+
PRINTNODE_PRINTER_ID = os.getenv('PRINTNODE_PRINTER_ID')
11+
12+
payload = request['body']
13+
14+
source_name = payload.get('source_name')
15+
if source_name.lower() == 'pos':
16+
return {
17+
'statusCode': 200,
18+
'body': json.dumps({'message': 'POS order, skipping print job.'})
19+
}
20+
21+
# Extract order information from Shopify webhook payload
22+
order_id = payload.get('id')
23+
order_total = payload.get('total_price')
24+
customer = payload.get('customer', {})
25+
customer_name = customer.get('first_name') + ' ' + customer.get('last_name')
26+
shipping_address = customer.get('default_address', {})
27+
28+
# Extracting shipping address details
29+
shipping_details = f"{shipping_address.get('address1', '')}, {shipping_address.get('city', '')}, {shipping_address.get('province', '')}, {shipping_address.get('country', '')}, {shipping_address.get('zip', '')}"
30+
31+
order_items = payload.get('line_items', [])
32+
33+
# Create a PDF document using fpdf2
34+
pdf = FPDF()
35+
pdf.add_page()
36+
pdf.set_font("Helvetica", size=12)
37+
38+
# Add content to PDF
39+
pdf.cell(200, 10, text=f"Order ID: {order_id}", new_x=XPos.LMARGIN, new_y=YPos.NEXT)
40+
pdf.cell(200, 10, text=f"Order Total: ${order_total}", new_x=XPos.LMARGIN, new_y=YPos.NEXT)
41+
pdf.cell(200, 10, text=f"Customer: {customer_name}", new_x=XPos.LMARGIN, new_y=YPos.NEXT)
42+
pdf.cell(200, 10, text=f"Shipping Address: {shipping_details}", new_x=XPos.LMARGIN, new_y=YPos.NEXT)
43+
pdf.cell(200, 10, text="Items:", new_x=XPos.LMARGIN, new_y=YPos.NEXT)
44+
45+
# Items List
46+
for item in order_items:
47+
pdf.cell(200, 10, text=f"Item: {item.get('name')}", new_x=XPos.LMARGIN, new_y=YPos.NEXT)
48+
pdf.cell(200, 10, text=f" - SKU: {item.get('sku')}", new_x=XPos.LMARGIN, new_y=YPos.NEXT)
49+
pdf.cell(200, 10, text=f" - Quantity: {item.get('quantity')}", new_x=XPos.LMARGIN, new_y=YPos.NEXT)
50+
pdf.cell(200, 10, text=f" - Price: ${item.get('price')}", new_x=XPos.LMARGIN, new_y=YPos.NEXT)
51+
52+
if 'variant_id' in item and item['variant_id']:
53+
pdf.cell(200, 10, text=f" - Variant ID: {item.get('variant_id')}", new_x=XPos.LMARGIN, new_y=YPos.NEXT)
54+
if 'variant_title' in item and item['variant_title']:
55+
pdf.cell(200, 10, text=f" - Variant Name: {item.get('variant_title')}", new_x=XPos.LMARGIN,
56+
new_y=YPos.NEXT)
57+
58+
# Save the PDF to a variable
59+
pdf_output = pdf.output()
60+
pdf_base64 = base64.b64encode(pdf_output).decode('utf-8')
61+
62+
print_job_payload = {
63+
"printerId": PRINTNODE_PRINTER_ID,
64+
"title": "Shopify Order",
65+
"contentType": "pdf_base64",
66+
"content": pdf_base64,
67+
"source": "Shopify Webhook"
68+
}
69+
70+
response = requests.post(
71+
'https://api.printnode.com/printjobs',
72+
auth=(PRINTNODE_API_KEY, ''),
73+
headers={'Content-Type': 'application/json'},
74+
data=json.dumps(print_job_payload)
75+
)
76+
77+
if response.status_code == 201:
78+
return {
79+
'statusCode': 200,
80+
'body': json.dumps({'message': 'Print job sent successfully!'})
81+
}
82+
else:
83+
return {
84+
'statusCode': 500,
85+
'body': json.dumps({'message': 'Failed to send print job.', 'error': response.text})
86+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import base64
2+
import json
3+
import os
4+
import requests
5+
from fpdf import FPDF, XPos, YPos
6+
7+
8+
def handler(request):
9+
PRINTNODE_API_KEY = os.getenv('PRINTNODE_API_KEY')
10+
PRINTNODE_PRINTER_ID = os.getenv('PRINTNODE_PRINTER_ID')
11+
12+
payload = request['body']
13+
14+
# Extract order information from Shopify webhook payload
15+
order_id = payload.get('id')
16+
order_total = payload.get('total_price')
17+
customer = payload.get('customer', {})
18+
customer_name = customer.get('first_name') + ' ' + customer.get('last_name')
19+
shipping_address = customer.get('default_address', {})
20+
21+
# Extracting shipping address details
22+
shipping_details = f"{shipping_address.get('address1', '')}, {shipping_address.get('city', '')}, {shipping_address.get('province', '')}, {shipping_address.get('country', '')}, {shipping_address.get('zip', '')}"
23+
24+
order_items = payload.get('line_items', [])
25+
26+
# Create a PDF document using fpdf2
27+
pdf = FPDF()
28+
pdf.add_page()
29+
pdf.set_font("Helvetica", size=12)
30+
31+
# Add content to PDF
32+
pdf.cell(200, 10, text=f"Order ID: {order_id}", new_x=XPos.LMARGIN, new_y=YPos.NEXT)
33+
pdf.cell(200, 10, text=f"Order Total: ${order_total}", new_x=XPos.LMARGIN, new_y=YPos.NEXT)
34+
pdf.cell(200, 10, text=f"Customer: {customer_name}", new_x=XPos.LMARGIN, new_y=YPos.NEXT)
35+
pdf.cell(200, 10, text=f"Shipping Address: {shipping_details}", new_x=XPos.LMARGIN, new_y=YPos.NEXT)
36+
pdf.cell(200, 10, text="Items:", new_x=XPos.LMARGIN, new_y=YPos.NEXT)
37+
38+
# Items List
39+
for item in order_items:
40+
pdf.cell(200, 10, text=f"Item: {item.get('name')}", new_x=XPos.LMARGIN, new_y=YPos.NEXT)
41+
pdf.cell(200, 10, text=f" - SKU: {item.get('sku')}", new_x=XPos.LMARGIN, new_y=YPos.NEXT)
42+
pdf.cell(200, 10, text=f" - Quantity: {item.get('quantity')}", new_x=XPos.LMARGIN, new_y=YPos.NEXT)
43+
pdf.cell(200, 10, text=f" - Price: ${item.get('price')}", new_x=XPos.LMARGIN, new_y=YPos.NEXT)
44+
45+
if 'variant_id' in item and item['variant_id']:
46+
pdf.cell(200, 10, text=f" - Variant ID: {item.get('variant_id')}", new_x=XPos.LMARGIN, new_y=YPos.NEXT)
47+
if 'variant_title' in item and item['variant_title']:
48+
pdf.cell(200, 10, text=f" - Variant Name: {item.get('variant_title')}", new_x=XPos.LMARGIN,
49+
new_y=YPos.NEXT)
50+
51+
# Save the PDF to a variable
52+
pdf_output = pdf.output()
53+
pdf_base64 = base64.b64encode(pdf_output).decode('utf-8')
54+
55+
print_job_payload = {
56+
"printerId": PRINTNODE_PRINTER_ID,
57+
"title": "Shopify Order",
58+
"contentType": "pdf_base64",
59+
"content": pdf_base64,
60+
"source": "Shopify Webhook"
61+
}
62+
63+
response = requests.post(
64+
'https://api.printnode.com/printjobs',
65+
auth=(PRINTNODE_API_KEY, ''),
66+
headers={'Content-Type': 'application/json'},
67+
data=json.dumps(print_job_payload)
68+
)
69+
70+
if response.status_code == 201:
71+
return {
72+
'statusCode': 200,
73+
'body': json.dumps({'message': 'Print job sent successfully!'})
74+
}
75+
else:
76+
return {
77+
'statusCode': 500,
78+
'body': json.dumps({'message': 'Failed to send print job.', 'error': response.text})
79+
}

0 commit comments

Comments
 (0)