After much research, I still can’t do it guys. My main goal is to translate a single PDF page in the free translation service in Google Translate with puppeteer in nodejs. It gave me the errors.
Here’s my code:
const puppeteer = require('puppeteer');
(async () => {
// Launch a headless browser
const browser = await puppeteer.launch();
// Open a new page
const page = await browser.newPage();
// Replace the URL below with the URL of the PDF you want to translate
const pdfUrl = 'https://www.cbu.edu/wp-content/uploads/2020/05/2019-20-ece-computersystems-cs.pdf';
// Navigate to the PDF translation page
await page.goto(`https://translate.google.com/translate?hl=en&sl=fr&u=${encodeURIComponent(pdfUrl)}`);
// Wait for the translation page to load
await page.waitForSelector('#contentframe iframe');
// Get the source URL of the translated content iframe
const iframeSrc = await page.evaluate(() => {
return document.querySelector('#contentframe iframe').src;
});
console.log('Found iframe:', iframeSrc);
// Navigate to the translated content iframe
await page.goto(iframeSrc);
// Wait for translation to complete
await page.waitForTimeout(5000); // Adjust the timeout as needed based on the translation complexity
// Print the translated content to PDF
await page.pdf({ path: '/tmp/translated_pdf.pdf' });
console.log('PDF translation completed successfully.');
// Close the browser
await browser.close();
})();
Here are the errors it gave me:
richardsonoge@richardsonoge-blooglet:/opt/lampp/htdocs/pdf_translator$ node translate_pdf.js
/opt/lampp/htdocs/pdf_translator/node_modules/puppeteer-core/lib/cjs/puppeteer/common/WaitTask.js:50
this.#timeoutError = new Errors_js_1.TimeoutError(`Waiting failed: ${options.timeout}ms exceeded`);
^
TimeoutError: Waiting for selector `#contentframe iframe` failed: Waiting failed: 30000ms exceeded
at new WaitTask (/opt/lampp/htdocs/pdf_translator/node_modules/puppeteer-core/lib/cjs/puppeteer/common/WaitTask.js:50:34)
at IsolatedWorld.waitForFunction (/opt/lampp/htdocs/pdf_translator/node_modules/puppeteer-core/lib/cjs/puppeteer/api/Realm.js:25:26)
at PQueryHandler.waitFor (/opt/lampp/htdocs/pdf_translator/node_modules/puppeteer-core/lib/cjs/puppeteer/common/QueryHandler.js:170:95)
at runNextTicks (node:internal/process/task_queues:60:5)
at process.processImmediate (node:internal/timers:442:9)
at async CdpFrame.waitForSelector (/opt/lampp/htdocs/pdf_translator/node_modules/puppeteer-core/lib/cjs/puppeteer/api/Frame.js:468:21)
at async CdpPage.waitForSelector (/opt/lampp/htdocs/pdf_translator/node_modules/puppeteer-core/lib/cjs/puppeteer/api/Page.js:1309:20)
at async /opt/lampp/htdocs/pdf_translator/translate_pdf.js:17:3
Node.js v18.13.0
richardsonoge@richardsonoge-blooglet:/opt/lampp/htdocs/pdf_translator$
How can I fix them and make this code work please? I don’t want you to just look at it and say nothing. Please, I want you all to take part in solving this problem for me…
Richardson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.