Comments (9)
I think currently there's no support for that.
In the mean time, you can use a custom template where you define the default font for your document. https://github.com/karnov/htmltoword#configure-templates-and-xslt-paths.
from htmltoword.
Thank you, but I am not sure how to use them (I tried). Do I have to create empty docx and point it there?
Where should I put this configuration (in what file)?
Htmltoword.configure do |config| config.custom_templates_path = config.default_templates_path = config.default_xslt_path = config.custom_xslt_path = end
from htmltoword.
It depends on your application, if it's a rails application you can have it in somewhere like config/initializers/htmltoword.rb
. Else you should be able to it like
require 'htmltoword'
# Configure the location of your custom templates
Htmltoword.config.custom_templates_path = 'some_path'
my_html = '<html><head></head><body><p>Hello</p></body></html>'
document = Htmltoword::Document.create(my_html, word_template_file_name)
file = Htmltoword::Document.create_and_save(my_html, file_path, word_template_file_name)
from htmltoword.
So for the time being we are stuck with one font and size for the entire document?
from htmltoword.
Been trying to work this out, but seems like even with the path custom_templates_path
created an initializer: config/initializers/htmltoword.rb
Htmltoword.configure do |config|
config.custom_templates_path = Rails.root.join('lib/assets/templates')
end
added debug output to the gem in the templates_helper.rb:
def template_file(template_file_name = nil)
default_path = File.join(::Htmltoword.config.default_templates_path, 'default.docx')
template_path = template_file_name.nil? ? '' : File.join(::Htmltoword.config.custom_templates_path, template_file_name)
p "Template path: " + template_path
File.exist?(template_path) ? template_path : default_path
end
"Template path: /lib/assets/templates/new_template.docx"
This path is correct and if I run:
open <the full path on disk to rails app folder>/lib/assets/templates/new_template.docx
it opens in word.
Even though this new_template.docx has overridden all fonts with Times New Roman it still creates a document with Arial for all text. Seems like the custom template is not working.
Also if I just update the default.docx in the gem to be Times New Roman it still generates a doc with Arial
from htmltoword.
any solution here?
from htmltoword.
I'm seeing the same issue - can't get htmltoword to use the template Word file I supply (via Rails 5.2). Has anyone actually got the templates to work or is it a universal problem?
I tried setting the paths in an initializer and, although this seems to work (the path is redefined), the default.docx
file at that location isn't used - even though it complains if the file isn't there.
I tried using the word_template option in the actual command with no effect (except that Rails complains if the file isn't there, of course, so I know the path is correct):
def download_project_file
respond_to do |format|
format.docx do
render docx: @project.file.filename.to_s.chomp('.docx')+' (output).docx', content: @project.file_contents, word_template: ActionController::Base.helpers.asset_path('/htmltoword/template.docx')
end
end
end
Is the only option to work out how to redefine the styles in the WordXML and rebuild the gem with my preferred styles? Has anyone tried this?
from htmltoword.
I just did the following, which worked:
- Upload https://github.com/karnov/htmltoword/blob/master/lib/htmltoword/templates/default.docx to Google Docs
- Edit the font and font sizes, then updating the document style e.g click "Update normal text to match" in google docs
- Download the modified version and add to our rails app at lib/htmltoword/templates/mytemplate.docx
- Create an initializer (config/initializers/htmltoword.rb) e.g
Htmltoword.configure do |config|
config.custom_templates_path = Rails.root.join('lib', 'htmltoword', 'templates')
end
- Reference template e.g Htmltoword::Document.create(html, 'mytemplate.docx')
from htmltoword.
Ah - thanks, @joshudev. The missing step for me was the last one: I'd tried in various ways to replace the default template and failed. Making an explicit template and referring to it at the point of generating the file gives me a file formatted as I wanted!
from htmltoword.
Related Issues (20)
- Heahers and Footers HOT 2
- Space inside elements HOT 1
- rails5.1.2 uninitialized constant Mime::DOCX HOT 1
- Text between <em> tags inside other tags not rendered in word HOT 1
- Use of label_tag :symbol generates corrupted .docx files
- Use a template from another directory HOT 1
- Retreive WordProcessingML After Conversion? HOT 1
- How Paste image to docx HOT 2
- Xml parsing error HOT 1
- error Unrecognized unit of measure when an image exist HOT 5
- List item bullets corrupted in Microsoft Word HOT 1
- Support to Insert Captions
- returns a .html file in response on calling API.
- Π‘an't convert large file (~100k rows in HTML) HOT 2
- Can't render images on the document HOT 5
- Can we apply CSS?
- Getting sporadic Net::OpenTimeout when rendering
- OpenURI::HTTPError for broken image link
- The default templates work in google doc but not with my custom_template HOT 3
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 htmltoword.