问题描述
我有一个PDF生成脚本,我在我的AdonisJS网站的前端中运行。 我使用的方法是将ajax请求发送到使用Puppeteer生成PDF的路由的循环。
我想做的是一项工作,将在我的网站上按一下按钮即可在后台运行。 作业开始运行后,允许该人关闭其浏览器。 同时在有限数量的并发线程上执行多任务,因为任务非常繁重。
当前,我使用服务器所有资源的方法是同时打开多个请求循环。但是,这效率低下,并非按队列排列,而且不受控制。
我希望我能从中学到关于解决方案的方向或参考。
我使用的下载PDF代码:
async downloadPDF(report_type,path,client,lang,response) {
let directory = reports_path + report_type + '/' + path + '/';
let url = generator_url + report_type + '/' + path + '/' + client + '/' + lang;
if (!fs.existsSync(directory)) { fs.mkdirSync(directory,{ recursive: true }); }
const browser = await Puppeteer.launch();
const page = await browser.newPage();
await page.goto(url,{waitUntil: 'networkidle0'});
const pdf = await page.pdf({
path: directory + client + '.pdf',format: 'A4',printBackground: true,margin: { left: "0px",top: "0px",right: "0px",bottom: "0px" }
});
await browser.close();
response.header('Content-Disposition','attachment;filename=' + client + '.pdf');
response.header('Content-Type','application/pdf');
response.send(pdf);
}
ajax循环:
<script>
const start_download = async(e) => {
link = domain + e.getAttribute("href");
await fetch(link).then(resp => resp.blob()).then(blob => {
// ... progressbar methods
}).catch(() => alert('Error!'));
}
function do_download() {
count = 0;
start();
}
const start = async () => {
await asyncForEach(dl_elements,async(e) => {
await start_download(e)
});
}
async function asyncForEach(array,callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index],index,array);
}
}
</script>
非常感谢您的帮助。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)