Skip to content

Commit 99cb3eb

Browse files
committed
docs: update iframe example
1 parent eff1b16 commit 99cb3eb

1 file changed

Lines changed: 60 additions & 11 deletions

File tree

examples/with-iframe/index.html

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<title>SimplePDF – Sending and Receiving events</title>
77
<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) {
914
const editor = document.getElementById("simplepdf");
10-
const input = document.getElementById("input");
1115

12-
const response = await fetch(input.value);
16+
const response = await fetch(url);
1317
const blob = await response.blob();
1418
const reader = new FileReader();
1519
await new Promise((resolve, reject) => {
@@ -18,10 +22,37 @@
1822
reader.readAsDataURL(blob);
1923
});
2024

25+
const [documentName] = url.substring(url.lastIndexOf('/') + 1).split('?');
26+
2127
editor.contentWindow.postMessage(
2228
JSON.stringify({
2329
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 },
2556
}),
2657
"*"
2758
);
@@ -43,7 +74,16 @@
4374
case "SUBMISSION_SENT":
4475
console.log("Event received:", payload);
4576
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;
4787
default:
4888
console.log("Unknown event received:", payload);
4989
return;
@@ -55,12 +95,21 @@
5595
</head>
5696
<body>
5797
<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>
64113
</div>
65114

66115
<iframe

0 commit comments

Comments
 (0)