Giter VIP home page Giter VIP logo

Comments (16)

edencorbin avatar edencorbin commented on July 20, 2024 2

This post helped me quite a bit, and I saw the recent question @Salindakrish, so I figured I would post the code that worked for me, perhaps its helpful:

app.get('/test', function (req, res){
    var docx = officegen ({
        'type': 'docx',
        'subject': 'testing it',
        'keywords': 'some keywords',
        'description': 'a description'
    });

    var pObj = docx.createP ();
    pObj.addText ( 'a very simple paragraph' );

    res.set({
        "Content-Type": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
        'Content-disposition': 'attachment; filename=blah.docx'
    });
    docx.generate(res);
});

from officegen.

jwerre avatar jwerre commented on July 20, 2024 1

Ya, I'll do that tomorrow and post it. Mocha tests be ok?

from officegen.

pietersv avatar pietersv commented on July 20, 2024

Quick checks, do the following work if you clone the repo and run these from the project root?

cd ~/MyProjects/
git clone https://github.com/Ziv-Barber/officegen.git
cd officegen
npm test // should return all passing

node example/make_docx.js
open out.docx // should open successfully

Can you post your code that generates the doc?

from officegen.

jwerre avatar jwerre commented on July 20, 2024

Ah... I set the 'a' flag on my write stream which was causing the error.

 var exportStream = fs.createWriteStream(@exportPath, {flags: 'a'})

from officegen.

jwerre avatar jwerre commented on July 20, 2024

I am actually still having this issue but I don't think it has anything directly to do with officegen.

If I generate the file directly with officegen I am able to open it:

var filename = 'my_doc.docx',
    ws = fs.createWriteStream(filename),
    docx = officegen({type: 'docx', onend:done}),
    p = docx.createP()
p.addText( data.survey.title, @styles.h1 )
docx.generate( ws )

The document generated here seems to work fine. However if I put it in an express route and download it via the web browser I get the above error.

router.get( "export/docx", function(req, res, next){
    res.download( filename )
})

The file seems to have the appropriate header information so I'm not sure what could be causing the problem:

HTTP/1.1 200 OK
X-Powered-By: Express
Content-Disposition: attachment; filename="my_doc.docx"
Accept-Ranges: bytes
Cache-Control: public, max-age=0
Last-Modified: Thu, 30 Jul 2015 20:17:44 GMT
ETag: W/"201d-14ee09d0640"
Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document
Content-Length: 8221
Date: Thu, 30 Jul 2015 20:17:44 GMT
Connection: keep-alive

Do you have any insight as to what might be causing the problem?

from officegen.

 avatar commented on July 20, 2024

Try using docx.generate(res)

   send: function (res, options) {

      res.set({
        "Content-Type": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
        'Content-disposition': 'attachment; filename=' + this.filename
      });
      this.pptx.generate(res);
    }

(sorry the above is pptx, not docx but it's an exact snippet from working code...)

from officegen.

jwerre avatar jwerre commented on July 20, 2024

I thought this might be the issue but I do have these headers set. See the header information above.

from officegen.

 avatar commented on July 20, 2024

The error message includes a backslash, might this be an issue with Windows vs OSX/Linux? Did you try having officegen stream the doc directly to the express res object vs streaming the file to disk and downloading it?

from officegen.

jwerre avatar jwerre commented on July 20, 2024

Yes, streaming it directly seems to to fix it. But I'm sure I don't have any backslashes in the the file name. What do you mean by "The error message includes a backslash"?

from officegen.

pietersv avatar pietersv commented on July 20, 2024

Glad that worked! Was just grasping at straws, based on this phrase below.

"...the filename might contain invalid characters (for example, <>|"/)"

Have in past in other (non-office) projects run into issues with files not transferring nicely between *nix and Windows. I wish Office provided more helpful diagnostic messages.

from officegen.

pietersv avatar pietersv commented on July 20, 2024

It seems you can generate a great matched tria of files for debugging:

  • One docx saved to disk on the file server, which opens fine on your machine.
  • That same file, sent by Express via res.download(file) and saved by client, which doesn't open.
  • That same docx, sent by Express via docx.generate(res) and saved by client, which does open.

Can you generate such a trio for debugging, minus any confidential info, and send directly? This is outside my domain, but I wrote a small a tool for differencing and isolating problems in Office files.

from officegen.

Salindakrish avatar Salindakrish commented on July 20, 2024

@jwerre did you manage to stream the file to the front end, I am having the same issue. If you succeeded please let me know...

from officegen.

Salindakrish avatar Salindakrish commented on July 20, 2024

thank you @edencorbin .. I did the same but how did you manage to retrieve it from the front end ,,, I may be having a little knowledge on it,, i used a Blob but it didn't work for me,,,,

`post("/generateppt", {req: '', res: ''})
.then(function(response) {
var pdfDoc = response.data;
var pdfBlob = new Blob([pdfDoc], { type: "application/vnd.openxmlformats-officedocument.presentationml.presentation" });
var pdfURL = window.URL.createObjectURL(pdfBlob);
var tempLink = document.createElement("a");
tempLink.href = pdfURL;
tempLink.setAttribute(
"download",
"Summary"
);
tempLink.setAttribute("target", "_blank");
document.body.appendChild(tempLink);
tempLink.click();
document.body.removeChild(tempLink);

        });`

But this gives a file but ,, after downloading it.. It shows a corrupted file... can you please assist me... I used front end javascript. I created a pptx...

from officegen.

edencorbin avatar edencorbin commented on July 20, 2024

@Salindakrish I'm not super knowledgeable either on officegen or the method you are attempting to pull it, so cannot help with your error. For me, the standard res from express worked, as shown in my code, although I have not tested it in many browsers, and likely will need to, to ensure compatibility. When ressing from express everything is set on the node/server side, and literally the browser just needs to hit the api url, no code on the client side.

from officegen.

Salindakrish avatar Salindakrish commented on July 20, 2024

@edencorbin Thanx mate. So did you bind that URL to a button click..??

from officegen.

ipopa avatar ipopa commented on July 20, 2024

@Salindakrish

angular example:

      $http({
                method: "GET",
                url: 'url/test',
                responseType: 'arraybuffer'
            })
            .then(function (rs) {
                saveAs(new Blob([rs.data], {type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"}), 'test.docx');
            });

from officegen.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.