Skip to content

Commit c0d66b3

Browse files
committed
fix: load local files via LOAD_DOCUMENT data URL instead of invalid localFile param
1 parent 92d9d3e commit c0d66b3

1 file changed

Lines changed: 27 additions & 15 deletions

File tree

examples/with-playwright-automation/src/automation.ts

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,19 @@ const setupIframePage = async ({
156156
};
157157
};
158158

159+
const isUrl = (value: string): boolean => value.startsWith('http://') || value.startsWith('https://');
160+
161+
const readFileAsDataUrl = ({ filePath }: { filePath: string }): string => {
162+
const absolutePath = path.isAbsolute(filePath) ? filePath : path.resolve(process.cwd(), filePath);
163+
164+
if (!fs.existsSync(absolutePath)) {
165+
throw new Error(`File not found: ${absolutePath}`);
166+
}
167+
168+
const buffer = fs.readFileSync(absolutePath);
169+
return `data:application/pdf;base64,${buffer.toString('base64')}`;
170+
};
171+
159172
const runAutomation = async ({ document, baseUrl }: { document: string; baseUrl: string }): Promise<AutomationResult> => {
160173
let browser: Browser | null = null;
161174

@@ -168,12 +181,25 @@ const runAutomation = async ({ document, baseUrl }: { document: string; baseUrl:
168181

169182
const page = await context.newPage();
170183

171-
const editorUrl = buildEditorUrl({ document, baseUrl });
184+
const editorUrl = isUrl(document)
185+
? `${baseUrl}/editor?open=${encodeURIComponent(document)}`
186+
: `${baseUrl}/editor`;
187+
172188
const { sendEvent, waitForEvent, waitForDocumentLoaded } = await setupIframePage({
173189
page,
174190
editorUrl,
175191
});
176192

193+
if (!isUrl(document)) {
194+
console.log('Waiting for editor to be ready...');
195+
await waitForEvent('EDITOR_READY');
196+
console.log('Editor ready, loading local file...');
197+
198+
const dataUrl = readFileAsDataUrl({ filePath: document });
199+
const fileName = path.basename(document);
200+
await sendEvent({ type: 'LOAD_DOCUMENT', data: { data_url: dataUrl, name: fileName } });
201+
}
202+
177203
console.log('Waiting for document to load...');
178204
await waitForDocumentLoaded();
179205
console.log('Document loaded');
@@ -208,18 +234,4 @@ const runAutomation = async ({ document, baseUrl }: { document: string; baseUrl:
208234
}
209235
};
210236

211-
const buildEditorUrl = ({ document, baseUrl }: { document: string; baseUrl: string }): string => {
212-
if (document.startsWith('http://') || document.startsWith('https://')) {
213-
return `${baseUrl}/editor?open=${encodeURIComponent(document)}`;
214-
}
215-
216-
const absolutePath = path.isAbsolute(document) ? document : path.resolve(process.cwd(), document);
217-
218-
if (!fs.existsSync(absolutePath)) {
219-
throw new Error(`File not found: ${absolutePath}`);
220-
}
221-
222-
return `${baseUrl}/editor?localFile=${encodeURIComponent(absolutePath)}`;
223-
};
224-
225237
export { runAutomation, AutomationResult };

0 commit comments

Comments
 (0)