请引用下面带有 Angular js 代码的简单 html
<!doctype html>
<html ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js"></script>
</head>
<body>
<div ng-controller="MyCtrl">
Hello, {{name}}!
</div>
<script type="text/javascript">
var myApp = angular.module('myApp',[]);
//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});
function MyCtrl($scope) {
$scope.name = 'Superhero';
}
</script>
</body>
</html>
当我在浏览器中打开此 html 时,它会正常工作。但我想将此 html 内容导出为 pdf。
读取 html 的所有内容并将内容写入 pdf 文件,但 Angular js 代码未在 pdf 中解析。
//reading the html content
string contents = File.ReadAllText(path);
//writing the html content to pdf
File.WriteAllText(path, contents);
但是 pdf 的输出就像
Hello, {{name}}!
这意味着它没有在 pdf 中解析。如何解决这个问题。
注意:我不想使用一些外部插件来生成 Angular JS 代码的 pdf。我需要用 C# 进行简单的读写。
最佳答案
之所以会出现这种情况,是因为您只是读取来自服务器的 HTML 响应,而不是调用页面上的 Javascript 后实际生成的内容。
您需要启动一个实际浏览器的实例来解释页面上的 Javascript,然后从那里读取输出。您可以尝试使用的一个 API 是 Selenium ,我在 Java 和 PHP 应用程序中都使用过它,但它们也有 .NET client driver
关于javascript - pdf 中的 Angular JS 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22047809/