javascript - pdf 中的 Angular JS 解析

标签 javascript html angularjs pdf

请引用下面带有 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/

相关文章:

javascript - HTML & CSS : Scroll on Y but stick on X

html - 为什么 1em 比默认字体小?

angularjs - 加载页面时如何默认选择一个单选按钮?

javascript - DOM 中的 $scope 值为 null

javascript - mongodb find() 在node.js中返回空结果

javascript - JS指数曲线拟合

javascript - 如何将多个参数从javascript转换为html表单

javascript - 单击具有固定标题的 anchor 时设置偏移量

javascript - 如何使用 javascript 或 jQuery 使用标签内已知的 html 内容查找标签的 ID?

javascript - 如何在 AngularJS 中使用 Cufon