|
| 1 | +import {authenticate} from '../shopify.server'; |
| 2 | + |
| 3 | +export async function loader({request}) { |
| 4 | + // [START route.authenticate] |
| 5 | + const {cors} = await authenticate.admin(request); |
| 6 | + // [END route.authenticate] |
| 7 | + |
| 8 | + // [START route.process-url] |
| 9 | + const url = new URL(request.url); |
| 10 | + const printTypes = url.searchParams.get('printTypes')?.split(',') || []; |
| 11 | + // [END route.process-url] |
| 12 | + |
| 13 | + // [START route.generate-html] |
| 14 | + const pages = printTypes.map((type) => createPage(type)); |
| 15 | + const print = printHTML(pages); |
| 16 | + // [END route.generate-html] |
| 17 | + |
| 18 | + // [START route.return-response] |
| 19 | + return cors( |
| 20 | + new Response(print, { |
| 21 | + status: 200, |
| 22 | + headers: { |
| 23 | + 'Content-type': 'text/html', |
| 24 | + }, |
| 25 | + }), |
| 26 | + ); |
| 27 | + // [END route.return-response] |
| 28 | +} |
| 29 | + |
| 30 | +// Helper function to create document pages based on type |
| 31 | +function createPage(type) { |
| 32 | + const email = '<!--email_off-->[email protected]<!--/email_off-->'; |
| 33 | + |
| 34 | + // Get document content based on type (invoice, packing slip, etc.) |
| 35 | + const getDocumentInfo = () => { |
| 36 | + switch (type) { |
| 37 | + case 'invoice': |
| 38 | + return { |
| 39 | + label: 'Receipt / Invoice', |
| 40 | + content: ` |
| 41 | + <p>Official Receipt/Invoice document</p> |
| 42 | + <p>Contains detailed payment and tax information</p> |
| 43 | + <p>Order details and pricing breakdown</p> |
| 44 | + `, |
| 45 | + }; |
| 46 | + case 'packing-slip': |
| 47 | + return { |
| 48 | + label: 'Packing Slip', |
| 49 | + content: ` |
| 50 | + <p>Shipping and fulfillment details</p> |
| 51 | + <p>Complete list of items in order</p> |
| 52 | + <p>Shipping address and instructions</p> |
| 53 | + `, |
| 54 | + }; |
| 55 | + case 'returns-form': |
| 56 | + return { |
| 57 | + label: 'Returns Form', |
| 58 | + content: ` |
| 59 | + <p>Return Authorization Form</p> |
| 60 | + <p>Return shipping instructions</p> |
| 61 | + <p>Items eligible for return</p> |
| 62 | + `, |
| 63 | + }; |
| 64 | + case 'draft-orders-quote': |
| 65 | + return { |
| 66 | + label: 'Draft Orders Quote', |
| 67 | + content: ` |
| 68 | + <p>Custom Order Quote</p> |
| 69 | + <p>Detailed pricing breakdown</p> |
| 70 | + <p>Terms and conditions</p> |
| 71 | + `, |
| 72 | + }; |
| 73 | + case 'refund-credit-note': |
| 74 | + return { |
| 75 | + label: 'Refund / Credit Note', |
| 76 | + content: ` |
| 77 | + <p>Refund Documentation</p> |
| 78 | + <p>Credit amount details</p> |
| 79 | + <p>Returned items list</p> |
| 80 | + `, |
| 81 | + }; |
| 82 | + default: |
| 83 | + return { |
| 84 | + label: type, |
| 85 | + content: ` |
| 86 | + <p>Sample document</p> |
| 87 | + <p>This is an example of a printable document.</p> |
| 88 | + `, |
| 89 | + }; |
| 90 | + } |
| 91 | + }; |
| 92 | + |
| 93 | + const {label, content} = getDocumentInfo(); |
| 94 | + |
| 95 | + return `<main> |
| 96 | + <div> |
| 97 | + <h1>${label}</h1> |
| 98 | + <div class="content"> |
| 99 | + ${content} |
| 100 | + <hr> |
| 101 | + <p>Contact us: ${email}</p> |
| 102 | + </div> |
| 103 | + </div> |
| 104 | + </main>`; |
| 105 | +} |
| 106 | + |
| 107 | +// Helper function to generate final HTML with proper styling and page breaks |
| 108 | +function printHTML(pages) { |
| 109 | + // Define page break styles for both screen and print |
| 110 | + const pageBreak = `<div class="page-break"></div>`; |
| 111 | + const pageBreakStyles = ` |
| 112 | + @media not print { |
| 113 | + .page-break { |
| 114 | + width: 100vw; |
| 115 | + height: 40px; |
| 116 | + background-color: lightgray; |
| 117 | + } |
| 118 | + } |
| 119 | + @media print { |
| 120 | + .page-break { |
| 121 | + page-break-after: always; |
| 122 | + } |
| 123 | + }`; |
| 124 | + |
| 125 | + const joinedPages = pages.join(pageBreak); |
| 126 | + return `<!DOCTYPE html> |
| 127 | + <html lang="en"> |
| 128 | + <head> |
| 129 | + <title>Print Document</title> |
| 130 | + <style> |
| 131 | + body, html { |
| 132 | + margin: 0; |
| 133 | + padding: 0; |
| 134 | + font-family: -apple-system, system-ui, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; |
| 135 | + } |
| 136 | + main { |
| 137 | + padding: 2rem; |
| 138 | + } |
| 139 | + h1 { |
| 140 | + margin: 0 0 1rem 0; |
| 141 | + font-size: 1.5rem; |
| 142 | + } |
| 143 | + .content { |
| 144 | + font-size: 1rem; |
| 145 | + line-height: 1.5; |
| 146 | + } |
| 147 | + hr { |
| 148 | + margin: 1.5rem 0; |
| 149 | + border: none; |
| 150 | + border-top: 1px solid #000; |
| 151 | + } |
| 152 | + ${pageBreakStyles} |
| 153 | + </style> |
| 154 | + </head> |
| 155 | + <body> |
| 156 | + ${joinedPages} |
| 157 | + </body> |
| 158 | + </html>`; |
| 159 | +} |
0 commit comments