A
A
Alexander2017-09-20 10:02:37
JavaScript
Alexander, 2017-09-20 10:02:37

How to use npm package on our website?

It is necessary to add the ability to save the page to a Word docx file on an existing site (PHP) I
looked at different options - I realized that it would be easiest to use something like officegen But
in general I can’t figure out how to use it on the server written npm install officegen And then the API immediately var officegen = require('officegen'); I'm missing something :) I can't figure out how to use it, do I need to compile it first? how to connect to the page? I'm dumb, please help :)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir, 2017-09-20
@Casufi

Too little information, where do you want to generate the document, on the server? Then the server should be written in node.js, if you do not want to rewrite the entire service, write a separate microservice for generating documents and access it from your PHP service.
https://learn.javascript.ru/screencast/nodejs
If it should work on the client, well then ask the author how you can connect the lib for use on the client.

A
Alexander N++, 2017-09-20
@sanchezzzhak

As Vladimir wrote
To generate and return the finished document, a basic http server must be created on nodejs

const
    finalhandler = require('finalhandler'),   // пакет нужно будет поставить через npm
    http = require('http'),
    officegen = require('officegen');  // пакет нужно будет поставить через npm
    Router = require('router'),  //  пакет нужно будет поставить через npm 
   // создаем сервер и биндим роутеры на все ответы
    server = http.createServer(function onRequest(req, res) {
          router(req, res, finalhandler(req, res));
    });  

// внутри метода пишем свой код с этой либы, я особо не вникал взял пример из документации
router.get('/generate-pptx', function(request,response) {
  response.writeHead ( 200, {
    "Content-Type": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
    'Content-disposition': 'attachment; filename=surprise.pptx'
    });

  var pptx = officegen ( 'pptx' );

  pptx.on ( 'finalize', function ( written ) {
      // ...
      });

  pptx.on ( 'error', function ( err ) {
      // ...
      });

  // ... (fill pptx with data)

  pptx.generate ( response );
});
// слушаем запросы на 3000 порту
server.listen({port: 3000});

Sitename:3000/generate-pptx
start the server any way i like pm2 start server.js
default response timeout in Nodejs is 2 minutes

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question