Comments (16)
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.
Ya, I'll do that tomorrow and post it. Mocha tests be ok?
from officegen.
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.
Ah... I set the 'a' flag on my write stream which was causing the error.
var exportStream = fs.createWriteStream(@exportPath, {flags: 'a'})
from officegen.
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.
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.
I thought this might be the issue but I do have these headers set. See the header information above.
from officegen.
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.
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.
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.
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.
@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.
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.
@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.
@edencorbin Thanx mate. So did you bind that URL to a button click..??
from officegen.
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)
- how to set header and footer inside docx using officegen HOT 1
- How to make a particular page Orientation to A3 / Landscape ? HOT 2
- How to add Sections in word file with Officegen ? and how to set font size and to make it bold for entire column in a table
- How to set the first line indentation for a paragraph in word?
- Can i remove Header and Footer from First page ( Title Page ) and some more queries
- Why does the table fill color not work?
- Cannot generate word file in render process for one Electron app
- How to add repeat header in table docx
- How to insert image into tableceil in docx
- how to open a word document ?
- Is addImage with svg supported for pptx file?
- How to assign color property to newline in table cell in word document ?
- 如何将word转换html,html里面的style css可以转换?
- Embedded File is still being accessed by createReadStream asynchronously even after deletion in officegen for Word Document
- Make the generated document read-only of password protected?
- Can I set a password to access Word
- How to add Number Bullet lists and Sub Number Bullet list
- How can i set auto-warp attribute to a column or cell in Excel file?
- Suggestion:Footer supports inserting tables
- May I ask if it is possible to add a background color to the Word document
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from officegen.