|
5 | 5 | <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
6 | 6 | <title>SimplePDF – Sending and Receiving events</title> |
7 | 7 | <script> |
8 | | - async function sendMessage() { |
| 8 | + |
| 9 | + function generateRandomID() { |
| 10 | + return Math.random().toString(36).substring(2, 15); |
| 11 | + } |
| 12 | + |
| 13 | + async function loadDocument(url) { |
9 | 14 | const editor = document.getElementById("simplepdf"); |
10 | | - const input = document.getElementById("input"); |
11 | 15 |
|
12 | | - const response = await fetch(input.value); |
| 16 | + const response = await fetch(url); |
13 | 17 | const blob = await response.blob(); |
14 | 18 | const reader = new FileReader(); |
15 | 19 | await new Promise((resolve, reject) => { |
|
18 | 22 | reader.readAsDataURL(blob); |
19 | 23 | }); |
20 | 24 |
|
| 25 | + const [documentName] = url.substring(url.lastIndexOf('/') + 1).split('?'); |
| 26 | + |
21 | 27 | editor.contentWindow.postMessage( |
22 | 28 | JSON.stringify({ |
23 | 29 | type: "LOAD_DOCUMENT", |
24 | | - data: { data_url: reader.result }, |
| 30 | + request_id: generateRandomID(), |
| 31 | + data: { data_url: reader.result, name: documentName }, |
| 32 | + }), |
| 33 | + "*" |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + function selectTool(value) { |
| 38 | + const editor = document.getElementById("simplepdf"); |
| 39 | + editor.contentWindow.postMessage( |
| 40 | + JSON.stringify({ |
| 41 | + type: "SELECT_TOOL", |
| 42 | + request_id: generateRandomID(), |
| 43 | + data: { tool: value }, |
| 44 | + }), |
| 45 | + "*" |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + function submit(downloadCopy) { |
| 50 | + const editor = document.getElementById("simplepdf"); |
| 51 | + editor.contentWindow.postMessage( |
| 52 | + JSON.stringify({ |
| 53 | + type: "SUBMIT", |
| 54 | + request_id: generateRandomID(), |
| 55 | + data: { download_copy: downloadCopy }, |
25 | 56 | }), |
26 | 57 | "*" |
27 | 58 | ); |
|
43 | 74 | case "SUBMISSION_SENT": |
44 | 75 | console.log("Event received:", payload); |
45 | 76 | return; |
46 | | - |
| 77 | + case 'EDITOR_READY': { |
| 78 | + const loadDocument = document.getElementById("load_document"); |
| 79 | + loadDocument.disabled = false; |
| 80 | + const selectTool = document.getElementById("select_tool"); |
| 81 | + selectTool.disabled = false; |
| 82 | + return; |
| 83 | + } |
| 84 | + case 'REQUEST_RESULT': |
| 85 | + console.log("Request result:", payload); |
| 86 | + return; |
47 | 87 | default: |
48 | 88 | console.log("Unknown event received:", payload); |
49 | 89 | return; |
|
55 | 95 | </head> |
56 | 96 | <body> |
57 | 97 | <div style="margin-bottom: 10px"> |
58 | | - <input |
59 | | - id="input" |
60 | | - value="https://cdn.simplepdf.com/simple-pdf/assets/example_en.pdf" |
61 | | - style="min-width: 400px" |
62 | | - /> |
63 | | - <button onclick="sendMessage()">Send "LOAD_DOCUMENT" message</button> |
| 98 | + <select onchange="loadDocument(this.value)" id="load_document" disabled> |
| 99 | + <option value="" selected>Load document</option> |
| 100 | + <option value="https://cdn.simplepdf.com/simple-pdf/assets/example_en.pdf">example_en.pdf</option> |
| 101 | + <option value="https://pdfobject.com/pdf/sample.pdf">sample.pdf</option> |
| 102 | + <option value="https://cdn.simplepdf.com/simple-pdf/assets/eu_%20competitiveness_2024.pdf">EU Competitiveness Report</option> |
| 103 | + <option value="https://cdn.simplepdf.com/simple-pdf/assets/eu_report_35mb_170pages.pdf">EU Report: 170 pages / 35mb</option> |
| 104 | + </select> |
| 105 | + <select onchange="selectTool(this.value)" id="select_tool" disabled> |
| 106 | + <option value="" selected>Select a tool</option> |
| 107 | + <option value="TEXT">Text</option> |
| 108 | + <option value="SIGNATURE">Signature</option> |
| 109 | + <option value="PICTURE">Picture</option> |
| 110 | + </select> |
| 111 | + <button id="submit" onclick="submit(false)">Submit</button> |
| 112 | + <button id="submit_with_download" onclick="submit(true)">Submit with Download</button> |
64 | 113 | </div> |
65 | 114 |
|
66 | 115 | <iframe |
|
0 commit comments