简体   繁体   English

如何使它们工作-NPM和Sails(html-pdf)

[英]How get them to work - npm & sails (html-pdf)

EDIT : Well I'll keep it simple and I'll ask all my other issues in other posts if need be. 编辑:好吧,我会保持简单,如果需要的话,我会在其他帖子中问我所有其他问题。 The original post was way too vast, hoping someone would answer me with a guide through all that needs to be known to use sails to its full potential. 最初的帖子太过庞大了,希望有人能向我提供有关使用帆发挥其全部潜能的所有知识的指南。 Sorry. 抱歉。

First of all, I'm quite new to angular, npm, and sails.js, but I have a very good Javascript base. 首先,我对angular,npm和sails.js还是很陌生,但是我有一个很好的Javascript基础。

All I know about those come from the 'Sails.js in Action' book. 我所知道的全部来自“ Sails.js in Action”书。

I'm trying to use an npm package (html-pdf to name it), but I can't get it to work. 我正在尝试使用npm软件包(用html-pdf命名),但无法正常工作。

I need to create a pdf invoice from the webpage. 我需要从网页创建pdf发票。

So I installed the html-pdf package via npm install html-pdf --save into my sails project. 所以我通过npm install html-pdf --save安装了html-pdf包到我的sails项目中。 Then just to test it out and get it to work, I used my home page which was currently blank and added some text and a 'save as pdf' button. 然后只是为了进行测试并使其正常工作,我使用了当前为空白的主页,并添加了一些文本和“另存为pdf”按钮。

Here is the ejs homepage code (in views) : 这是ejs主页代码(在视图中):

<div ng-controller="homePageCtrl" class="homepage" ng-cloak>
    <div class="container">
      <div class="row">
        <center>
            <br /><h1>Accueil</h1><br /><br />
            <h3>Bienvenue !</h3><br /><br />
            <h3>Test création PDF</h3><br />
            <h4>Si vous avez des questions ou des problèmes,</h4>
            <h4>veuillez contacter l'assistance technique.</h4>
            <br /><br />
            <button class="btn btn-success btn-lg" type="button" ng-click="printPdf()">
              <span>Imprimer</span>
              </button>
        </center>
      </div>
    </div>
  </div>

Now here is the first thing I tried (quite a copy of the example code) for the printPdf function from my assets/js/controllers/homePageCtrl.js : 现在这是我尝试从我的assets / js / controllers / homePageCtrl.js中为printPdf函数尝试的第一件事(相当一部分示例代码):

angular.module('teknik').controller('homePageCtrl', ['$scope', '$http', function($scope, $http) {

    $scope.printPdf = function(){
      var fs = require('fs');
      var pdf = require('html-pdf');
      var html = fs.readFileSync('./views/homepage.ejs', 'utf8');
      var options = { format: 'Letter' };

      pdf.create(html, options).toFile('/pdfs/test.pdf', function(err, res) {
        if (err) return console.log(err);
        console.log(res); 
      });
    };

}]);

It gives me an error on the var fs = require('fs'); 它给我一个关于var fs = require('fs');的错误var fs = require('fs'); line (Can't find variable: require) so I tried a few changes afterward. 行(找不到变量:require),所以我后来尝试了一些更改。 I found this example Sails EJS-view after html-pdf usage does not render image and tried to change the code to fit my needs but it also gave me an error on the beginning of the code (same error but I don't have that adaptation anymore). 在html-pdf用法未呈现图像后,我发现此示例Sails EJS-view并尝试更改代码以适合我的需要,但它在代码开头也给了我一个错误(相同的错误,但我没有适应)。

I'm no expert on this topic, but I think I can see exactly the gap in your knowledge that's giving you so much trouble. 我不是这个主题的专家,但是我想我可以确切地看到您的知识差距给您带来了很多麻烦。

npm modules, and most of the documentation that goes with them, are geared towards using the modules in code run on the server . npm模块以及随附的大多数文档都旨在在服务器上运行代码中使用这些模块。 From what I can see in your code above, you are running the code in the client's browser . 从上面的代码我可以看到,您正在客户端浏览器中运行代码。 Npm modules and the whole require framework they use is not set up there. 在那里没有设置Npm模块以及它们使用的整个require框架。

In Sails, the code you write in your controllers, services, and models (pretty much anything in your /api/ folder is run server-side, so requiring and using npm modules will work there. 在Sails中,您在控制器,服务和模型中编写的代码( /api/文件夹中的几乎所有内容都在服务器端运行,因此要求和使用npm模块将在此处运行。

Code you put in your /assets/ folder is different. 您放在/assets/文件夹中的代码是不同的。 Files here have javascript that is sent (without being run) to the browser on a client machine and run there. 此处的文件具有javascript,该javascript被发送(不运行)到客户端计算机上的浏览器并在该计算机上运行。 Npm modules are not set up to work there. 没有设置Npm模块在那儿工作。

Sails views are a bit of a confusing case: the templating is done on the server side (all the <% ... %> blocks are evaluated), but none of the javascript outside those blocks is run until a client browser runs it later. Sails视图有点令人困惑:在服务器端完成模板(所有<% ... %>块均已评估),但这些块外的所有javascript都不会运行,直到客户端浏览器稍后运行它为止。 For example, event handler code is all client-side. 例如,事件处理程序代码是所有客户端代码。

If you do need libraries etc that run client side, you can do it the "old fashioned" way: add a javascript asset, and link to it from the file where you need to run it, same as jquery, etc. So, if you have the file /assets/js/some_library.js in your project, then in an html file or view, you can add: 如果确实需要运行客户端的库等,则可以采用“老式”的方法:添加一个javascript资产,并从需要运行它的文件(与jquery一样)中链接到它。因此,如果您在项目中拥有文件/assets/js/some_library.js ,然后在html文件或视图中添加:

<script src="/js/some_library.js"></script>
<script>
    // your custom code here. You can often use
    // variables from the library here as you would
    // after "require"ing them in server-side code
</script>

Npm can be an awkward place to find scripts to use in this way. Npm可能是查找以这种方式使用的脚本的尴尬场所。 Sometimes you can dig into the npm folder and find a file in a lib or dist folder that you can use like I've used some_library.js above, but not always. 有时,您可以进入npm文件夹并在libdist文件夹中找到一个文件,您可以使用该文件,就像我在上面使用some_library.js ,但并非总是如此。 There is probably a more browser-oriented source for any of the functions you are trying to get from npm. 您尝试从npm获取的任何功能可能都有面向浏览器的来源。

Hope this helps! 希望这可以帮助!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM