Coder Social home page Coder Social logo

Comments (15)

Michael-Huang-9001 avatar Michael-Huang-9001 commented on May 13, 2024 1

Welp, I circumvented the express packer by using a local packer and downloading that file.

Thanks anyway, awesome module you've created! Very neat. I was using Officegen but that one didn't have header/footer image support, many thanks for that.

:]

Here are some of the problems I encountered while using this module:
ExpressPacker.packPdf() isn't a function for some reason.
The Header error in ExpressPacker crashing Node after a successful download.
LocalPacker.packPdf does indeed pack a valid PDF, but it ignored image.scale changes in the PDF.

from docx.

dolanmiu avatar dolanmiu commented on May 13, 2024

That is not supported yet

Will add as a feature request

from docx.

Michael-Huang-9001 avatar Michael-Huang-9001 commented on May 13, 2024

It seems that I can make them fit to the margins by setting the cell width to something large like 20000.

I have another question, how can I anchor a picture to the top of a header/footer?
I can't seem to win against the margins at the top of the header/bottom of the footer.

Also, a funny thing, if I tried to insert a picture into the footer and the path to the file or the file has a space in its name, it corrupts the output for some reason. This occurs in the body and header/footer.

from docx.

Michael-Huang-9001 avatar Michael-Huang-9001 commented on May 13, 2024

It seems for my use case, I simply need the image in the header to be wrapped behind text and move to a specified location.

Is there anything that this module can do on that front?

from docx.

dolanmiu avatar dolanmiu commented on May 13, 2024

Do you mean fixed image positioning? And then placing that image behind the text?

If so, fixed positioning of images are coming soon! The code is there, just need to release

from docx.

Michael-Huang-9001 avatar Michael-Huang-9001 commented on May 13, 2024

Hi, thanks for being so helpful so far. This module is great.
I have one final small question.
My express packer is throwing out errors and when I call:

router.post('/', (req, res) => {
...
const express_exporter = new docx.ExpressPacker(doc, res);
express_exporter.pack('docx');
});

It works fine with a localpacker, but the express packer gives this error:

_http_outgoing.js:471
throw new ERR_HTTP_HEADERS_SENT('set');
^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:471:11)
at ServerResponse.header
at ServerResponse.send
at ServerResponse.ExpressPacker.res.on
at ServerResponse.emit (events.js:187:15)
at emitCloseNT (_http_server.js:589:8)
at process._tickCallback (internal/process/next_tick.js:63:19)

The downloaded file says it's corrupted too.
Any ideas? Thanks.

from docx.

Michael-Huang-9001 avatar Michael-Huang-9001 commented on May 13, 2024

Also, it seems the PDFpacker ignores any picture scaling I've made.

from docx.

dolanmiu avatar dolanmiu commented on May 13, 2024

It looks like you are calling some express related stuff along with pack()

Are you calling res.send(), or res.end() by any chance?

from docx.

Michael-Huang-9001 avatar Michael-Huang-9001 commented on May 13, 2024

I'm not calling those at all.

Maybe because I'm using the version in npm but I'm not sure if that's even the issue.
Is the npm package outdated or something?

from docx.

dolanmiu avatar dolanmiu commented on May 13, 2024

Check out these RunKit examples I've got on the README, its all express, and it works like a charm

https://runkit.com/dolanmiu/docx-demo1

from docx.

Michael-Huang-9001 avatar Michael-Huang-9001 commented on May 13, 2024

Okay, I got the express packer spit out a download that is not corrupted but I still cannot seem to resolve that error regarding the set header.

I just can't seem to get rid of that error and try/catch doesn't seem to stop it from crashing the Node process.

To be more specific, this is what my router for the download looks like:

let script = require('../Report generator');

router.post("/download/docx/:id", (req, res) => {
    console.log('-- Downloading DOCX...');

    let report_json = results;

    async function get_report() {
        // Setting some json from post request
        return await script.generate_report(report_json, res);
    }

    if (req.params && req.params.id) {
        console.log("params.id: " + req.params.id);
        if (req.body.generate_report && report_json) {
            console.log("Report generation flag OK");

            get_report().then((success) => {
                if (success) 
                    console.log("back in router, successful");
           }
        } else {
            console.log("Generate report flag not set, invalid state.");
        }
    }
});

My report generation works, and it downloads a proper DOCX report, but the express packer seem to just kill the node process after download with the header bug:

_http_outgoing.js:471
    throw new ERR_HTTP_HEADERS_SENT('set');
    ^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:471:11)
    at ServerResponse.header 
    at ServerResponse.send 
    at ServerResponse.ExpressPacker.res.on 
    at ServerResponse.emit (events.js:187:15)
    at emitCloseNT (_http_server.js:589:8)
    at process._tickCallback (internal/process/next_tick.js:63:19)

I also replaced my get index (router.get('/')) with your example line for line, and it does download a DOCX but it also faces the same header issue.

from docx.

Michael-Huang-9001 avatar Michael-Huang-9001 commented on May 13, 2024

Hmmm...
I think the header issue is because I called some next() and that causes it to call some middleware to call res.send or res.end somewhere.
Is it possible to not call res.end in the expresspacker?
Officegen had a streamer and I just set an attachment.

//Officegen's method
await docx.generate(res);
console.log("DOCX streamed to res.");
res.set({"Content-Type": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", 'Content-disposition': 'attachment; filename=' + filename'});

from docx.

dolanmiu avatar dolanmiu commented on May 13, 2024

Good suggestion, will add in the major version of docx and this would be breaking

from docx.

simonmthiel avatar simonmthiel commented on May 13, 2024

+1 for this feature request

from docx.

dolanmiu avatar dolanmiu commented on May 13, 2024

Fixed in #288

API will be like so:

https://github.com/dolanmiu/docx/blob/master/demo/demo32.ts#L59-L64

Will be released in v5

from docx.

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.