Repository navigation
Best Practices
Hammad Maqbool edited this page Oct 29, 2026
·
1 revision
English · 简体中文 · 繁體中文 · Español · العربية · Français · Português (Brasil) · Deutsch · हिन्दी · 日本語 · 한국어 · Русский · Bahasa Indonesia · Türkçe · Italiano · Tiếng Việt
These habits come straight from how docxi works, and most of them prevent a problem listed in Troubleshooting. They're grouped by topic. The page setup advice applies to everyone, and the security and performance advice matters most for production apps.
-
Pass all four margins together. The hash you pass to
Docxi::Document.newreplaces the defaults, and any oftop,right,bottomandleftyou leave out is written empty. -
Set the first-page header and footer. docxi always turns on a separate first page. If page 1 needs a header or footer, call
add_first_headerandadd_first_footeras well.
-
Use
space: truefor leading or trailing spaces. Otherwise Word drops them and the words of two runs touch. -
Put shared formatting on the paragraph. Options given to
p, such assize:orfont:, apply to every run inside it, so there's less to repeat. -
Use
p.brfor line breaks anddocument.br(page: true)for page breaks.document.brwithoutpage: trueproduces XML that isn't valid.
-
Set borders on rows or cells, not on the table. In 0.1.0, table-level
borders:are applied to every cell rather than to the outside of the table. -
Give
columns_widthfor every column. Cells take their width from it, so tables keep the layout you expect.
- Use PNG files. Every image is stored as a PNG part.
-
Size inline images with
style:, for examplestyle: { width: "120pt", height: "60pt" }. It's the one image size that works as you'd expect in 0.1.0. -
Pass a path rather than an open
Fileif the document might be rendered more than once. IO images are read duringrender, and a second render gets an empty image. -
Header, table-cell and positioned images until 0.2.0. Their width and height are both computed as
width * height * 14.92EMU, so they come out square. To get a square image ofSinches, passwidthandheightequal toMath.sqrt(S * 914400 / 14.92).round, which is about 248 for one inch.
-
Don't pass untrusted strings to
add_media. A path makes docxi read that file from your server, and a URL makes your server download it. Pass an IO you opened yourself, or check the value first. See Security. -
Text is escaped for you. Strings passed to
textare written as XML text, so user input can't change the document structure.
-
Build big documents in a background job. The whole document is built in memory when you call
render. See Integrations. -
Render once, then reuse the bytes. Keep
word.render.stringif you need to send or store the document more than once. -
Pin a compatible version with
gem "docxi", "~> 0.1". -
Test what you generate. A
.docxis a zip of XML files, so tests can unzip it and check the XML. docxi's own tests work this way. See Architecture.
docxi wiki · README · Installation · Troubleshooting · Report an issue · MIT License
Start
Build documents
Help
Project
Links
Languages