简体   繁体   English

如何使用 jsPDF 将作为 Fetch API 响应的数据写入我的 pdf 中?

[英]How to write the data received as response from the Fetch API into my pdf using jsPDF?

I am trying to write the title of a website into my pdf.我正在尝试将网站标题写入我的 pdf。 This is my code:这是我的代码:

const doc = new jsPDF();

fetch('https://api.codetabs.com/v1/proxy?quest=https://arunachaltimes.in/index.php/2022/04/27/mein-requests-vaishnaw-to-start-murkongselek-pasighat-broad-gauge-project/')
    .then(
        function (response) {
            response.text().then(function(data) {

                const parser = new DOMParser();
                const htmldoc = parser.parseFromString(data, "text/html");
                let title = htmldoc.getElementsByTagName("title")[0].innerText;
               
                console.log(title) //shows the correct value

                doc.text(10,10, title);
            })
        }
     )
    .catch(function(err) {
         console.log(err);
    })

doc.save('myfile.pdf'); //completely blank pdf

The pdf is completely empty?! pdf 完全是空的?! Why?为什么? When I console.log(title) , then the value shows, but I think the pdf is downloaded before the text is written into the pdf.当我console.log(title) ,然后值显示,但我认为 pdf 是在文本写入 pdf 之前下载的。 How do I fix it?我如何解决它?

try this, doc.text( title,10,10);试试这个,doc.text(title,10,10); doc.text( title,10,10); doc.text(标题,10,10);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过ajax提取api响应将从laravel控制器接收的数据发送到另一页 - how to send data received from laravel controller to another page through ajax fetch api response 如何评估在提取API中收到的HTTP响应 - how to evaluate http response received in fetch api 如何使用 javascript 从 API 下载内联 pdf? - How to download an inline pdf received from API using javascript? 如何在使用 JSPDF 新 html API 从 html 生成 pdf 时给出宽度、高度、x 和 y 坐标 - How to give width, height, x and y coordinates in generating pdf from html using JSPDF new html API 使用二进制数据使用AJAX从jsPDF上传PDF - Uploading PDF from jsPDF with AJAX using binary data 如何使用 fetch 从一个模块导出从 GET API 获得的响应数据到另一个模块 - How to export the response data obtained from a GET API using fetch from one module to another 如何使用jsPDF为PDF文档中的动态数据添加自定义字体? - How to add a Custom Font to Dynamic data in PDF document by using jsPDF? 如何在模型中的.fetch()之后写入从服务器接收的数据并传递给模板? - How to write data received from the server after .fetch() in model and passed to template? 如何使用 node-fetch 从 API 获取数据和响应状态? - How to get data and response status from API using node-fetch? 如何使用 axios 获取和映射 api 响应数据 - How to fetch and map api response data using axios
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM