[英]How to read a html file in JavaScript?
I am working on a blazorWebAssmebly project.我正在从事 blazorWebAssmebly 项目。
I want to convert html to pdf, for that I am using jsPDF ( javascript).我想将 html 转换为 pdf,因为我正在使用 jsPDF (javascript)。
function generatePDF() {
const element ="here_I_want_to_read_the_Html_file"; //
var opt = {
margin: 1,
filename: 'html2pdf_example.pdf',
image: { type: 'jpeg', quality: 0.98 },
html2canvas: { scale: 2 },
jsPDF: { unit: 'in', format: 'letter', orientation: 'portrait' }
};
// Choose the element that our invoice is rendered in.
html2pdf().set(opt).from(element).save();
}
The folder structre文件夹结构
-wwwroot -wwwroot
-js
-generatePDF.js
-html
-pdfTempltor.html
How can I read the html file inside javaScript file如何读取 javaScript 文件中的 html 文件
You cannot directly access to server files with JavaScript. You have to create a webservice in the backend and call it with JavaScript. JavaScript不能直接访问服务器文件,必须在后台创建一个webservice,然后用JavaScript调用。
you fetch the html file, put it in your const element...你获取 html 文件,把它放在你的 const 元素中......
function to fetch file function 取文件
function loadHTML(file, callback) {
fetch(file)
.then((response) => response.text())
.then((respText) => {
if (callback && typeof (callback) === "function") {
callback(respText);
}
});
}
calling the function拨打 function
loadHTML('your html file with the right path from here', function (data) {
const element = data;
...
your code
...
}
Or another structure if you prefer, but the fetch will be more or less the same如果您愿意,也可以使用另一种结构,但获取方式或多或少是相同的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.