Code Monkey home page Code Monkey logo

htmltoword's Introduction

Ruby Html to word Gem

This simple gem allows you to create MS Word docx documents from simple html documents. This makes it easy to create dynamic reports and forms that can be downloaded by your users as simple MS Word docx files.

Add this line to your application's Gemfile:

gem 'htmltoword'

And then execute:

$ bundle

Or install it yourself as:

$ gem install htmltoword

Note: Since version 0.4.0 the create method will return a string with the contents of the file. If you want to save the file please use create_and_save. See the usage for more

Security warnings

In versions 0.7.0 and 1.0.0 we introduced a security vulnerability when allowing the use of local images since no check to the files was done, potentially exposing sensitive files in the output zipfile.

Version 1.1.0 doesn't allow the use of local images but uses an insecure open

Usage

Standalone

By default, the file will be saved at the specified location. In case you want to handle the contents of the file as a string and do what suits you best, you can specify that when calling the create function.

Using the default word file as template

require 'htmltoword'

my_html = '<html><head></head><body><p>Hello</p></body></html>'
document = Htmltoword::Document.create(my_html)
file = Htmltoword::Document.create_and_save(my_html, file_path)

Using your custom word file as a template, where you can setup your own style for normal text, h1,h2, etc.

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)

The create function will return a string with the file, so you can do with it what you consider best. The create_and_save function will create the file in the specified file_path.

With Rails

For htmltoword version >= 0.2 An action controller renderer has been defined, so there's no need to declare the mime-type and you can just respond to .docx format. It will look then for views with the extension .docx.erb which will provide the HTML that will be rendered in the Word file.

# On your controller.
respond_to :docx

# filename and word_template are optional. By default it will name the file as your action and use the default template provided by the gem. The use of the .docx in the filename and word_template is optional.
def my_action
  # ...
  respond_with(@object, filename: 'my_file.docx', word_template: 'my_template.docx')
  # Alternatively, if you don't want to create the .docx.erb template you could
  respond_with(@object, content: '<html><head></head><body><p>Hello</p></body></html>', filename: 'my_file.docx')
end

def my_action2
  # ...
  respond_to do |format|
    format.docx do
      render docx: 'my_view', filename: 'my_file.docx'
      # Alternatively, if you don't want to create the .docx.erb template you could
      render docx: 'my_file.docx', content: '<html><head></head><body><p>Hello</p></body></html>'
    end
  end
end

Example of my_view.docx.erb

<h1> My custom template </h1>
<%= render partial: 'my_partial', collection: @objects, as: :item %>

Example of _my_partial.docx.erb

<h3><%= item.title %></h3>
<p> My html for item <%= item.id %> goes here </p>

For htmltoword version <= 0.1.8

# Add mime-type in /config/initializers/mime_types.rb:
Mime::Type.register "application/vnd.openxmlformats-officedocument.wordprocessingml.document", :docx

# Add docx responder in your controller
def show
  respond_to do |format|
    format.docx do
      file = Htmltoword::Document.create params[:docx_html_source], "file_name.docx"
      send_file file.path, :disposition => "attachment"
    end
  end
end
  // OPTIONAL: Use a jquery click handler to store the markup in a hidden form field before the form is submitted.
  // Using this strategy makes it easy to allow users to dynamically edit the document that will be turned
  // into a docx file, for example by toggling sections of a document.
  $('#download-as-docx').on('click', function () {
    $('input[name="docx_html_source"]').val('<!DOCTYPE html>\n' + $('.delivery').html());
  });

Configure templates and xslt paths

From version 2.0 you can configure the location of default and custom templates and xslt files. By default templates are defined under lib/htmltoword/templates and xslt under lib/htmltoword/xslt

Htmltoword.configure do |config|
  config.custom_templates_path = 'path_for_custom_templates'
  # If you modify this path, there should be a 'default.docx' file in there
  config.default_templates_path = 'path_for_default_template'
  # If you modify this path, there should be a 'html_to_wordml.xslt' file in there
  config.default_xslt_path = 'some_path'
  # The use of additional custom xslt will come soon
  config.custom_xslt_path = 'some_path'
end

Features

All standard html elements are supported and will create the closest equivalent in wordml. For example spans will create inline elements and divs will create block like elements.

Highlighting text

You can add highlighting to text by wrapping it in a span with class h and adding a data style with a color that wordml supports (http://www.schemacentral.com/sc/ooxml/t-w_ST_HighlightColor.html) ie:

<span class="h" data-style="green">This text will have a green highlight</span>

Page breaks

To create page breaks simply add a div with class -page-break ie:

<div class="-page-break"></div>

Images

Support for images is very basic and is only possible for external images(i.e accessed via URL). If the image doesn't have correctly defined it's width and height it won't be included in the document

Limitations:

  • Images are external i.e. pictures accessed via URL, not stored within document
  • only sizing is customisable

Examples:

<img src="http://placehold.it/250x100.png" style="width: 250px; height: 100px">
<img src="http://placehold.it/250x100.png" data-width="250px" data-height="100px">
<img src="http://placehold.it/250x100.png" data-height="150px" style="width:250px; height:100px">

Contributing / Extending

Word docx files are essentially just a zipped collection of xml files and resources. This gem contains a standard empty MS Word docx file and a stylesheet to transform arbitrary html into wordml. The basic functioning of this gem can be summarised as:

  1. Transform inputed html to wordml.
  2. Unzip empty word docx file bundled with gem and replace its document.xml content with the new transformed result of step 1.
  3. Zip up contents again into a resulting .docx file.

For more info about WordML: http://rep.oio.dk/microsoft.com/officeschemas/wordprocessingml_article.htm

Contributions would be very much appreciated.

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

(The MIT License)

Copyright © 2013:

  • Cristina Matonte

  • Nicholas Frandsen

htmltoword's People

Contributors

anitsirc avatar benbalter avatar biremi avatar cduez avatar filipkis avatar fran-worley avatar francois2metz avatar guille-moe avatar jeppeliisberg avatar jharbert avatar mdh avatar nickfrandsen avatar robingram avatar stats avatar tobiashm avatar vyruss avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

htmltoword's Issues

Support to Insert Captions

I need to create Word reports that contains dozens of images, and is very important to create a table of figures, but have been trying to include captions but nothing seems to work.

Is there a current way to achieve this?

How to change path of template?

I have to use this code in application.rb:

Htmltoword.configure do |config|
  config.custom_templates_path = 'C:\web\Rails\project\app\views\documents'
end

with this code in the controller.rb:

format.docx do
        render docx: 'show', word_template: 'mytemplate.docx'
      end

Or I can do it better?

Because in the "config.custom_templates_path" I don't want the absolute path!

Lines containing both strong and br tags, get trimmed

The content gets trimmed whenever a line contains both strong and br tags.

For example

<html>
<head></head>
<body>
    <h1>Title level 1</h1>
    <hr>
    <h2>Title level 2</h2>

    <h3>Title level 3</h3>
    <div>
        <p>
            <strong>Bold label one</strong> - around 120.<br>
            <strong>Bold label three</strong> - around 90.<br>
            One more line here
        </p>
    </div>
</body>
</html>

Removes everything after the br tag, but only for the current p tag. Here is the result:
screen shot 2015-12-03 at 11 56 32

But once I remove the strong tags, everything gets rendered as expected.

<html>
<head></head>
<body>
    <h1>Title level 1</h1>
    <hr>
    <h2>Title level 2</h2>
    <h3>Title level 3</h3>
    <div>
        <p>
            Bold label one - around 120.<br>
            Bold label two - around 90.<br>
            One more line here
        </p>
    </div>
</body>
</html>

screen shot 2015-12-03 at 11 57 05

Default template contains misleading metadata

The default template contains some misleading metadata:

screen shot 2015-05-28 at 1 24 14 pm

Specifically, the "template", and author, as well as some properties that aren't necessary:

screen shot 2015-05-28 at 1 25 42 pm

While it doesn't affect the output, it can be a bit misleading when sending the created document to third-parties.

Glad to submit a pull request, but wanted to check in first before I did, in case it was purposeful.

Extra Vertical Whitespace

The following HTML tags generate unwanted paragraph breaks ("[P]"):

  1. "some
    text" becomes "some [P][P] text" (2 line breaks, where I only want one)
  2. "bold text" becomes "[P]bold text[P]" (2 line breaks where I want zero)
  3. "italic text" becomes "[P]italic text[P]" (2 line breaks where I want zero)

Possible Cause: Examining an output docx shows that the tags (
, , ) are treated as distinct paragraphs. i.e. the current paragraph terminates, the gets wrapped in <w:p> wordml tags, then another paragraph is started for whatever text comes immediately after the closing .

Use case: Including bold or italic text in an AMA-style journal citation.

Environment info: Htmltoword 0.4.2, Rails 4.1.5, Ruby 2.1.5. Issue replicated on both OSX Yosemite and Ubuntu 14.04.

 [update] - Newlines in the input affect the output too.
 Probably a consequence of xslt treating all input as an XML document,
 since that's what XSLT is designed to do.

 e.g. "some text" shows in the output docx as "some text", but 
 "some 
 text"
 becomes "some[CR]text" in the docx.

create_and_save creates corrupted docx files on win32 platforms

Using latest version available on git (0.4.4), and create a simple ruby program.

require 'htmltoword'
file = Htmltoword::Document.create_and_save( "<html><head><title>Simple title</title></head><body>Hello docx</body></html>", "out-001.docx" )

Running this on Mac OS-X works, fine and generates a docx file that can be opened. However generating and opening the same file on Win32 (Windows XP) results in a corrupted file.

Ruby version = 2.0.0p195 (but could be tested on more recent rubies)

Issue with `p` containing formatted text and a br

There seems to be something wrong when using formatted text and a br inside the same p tag. A minimal reproduction looks like this:

<p><strong>bold:</strong> text.<br />after br</p>

The resulting word document looks like this:

screen shot 2015-12-17 at 10 46 12

and the WordprocessingML looks like this:

<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage" xmlns:str="http://exslt.org/strings" xmlns:fn="http://www.w3.org/2005/xpath-functions">
  <w:r>
    <w:rPr>
      <w:b/>
    </w:rPr>
    <w:t xml:space="preserve">bold:</w:t>
  </w:r>
  <w:r>
    <w:t xml:space="preserve"> text.</w:t>
  </w:r>
</w:p>

Documentation/Examples/Pdf

Hi Guys,

I can see the gem has a lot of features. It would be great to have more detailed examples and even link to rich examples so that it is easier to understand. In addition, why not to add an option for pdf support to enhance the scope of the gem?

ALL CAPS?

I'm getting started with your gem and ran into some issues. The main thing is the template. It looks like most of the default header tags are ALL CAPS. I tried using my own template file, but it is silently ignored.

If a word_template: "foo.docx" is not found, I think you should throw an exception to help users find their problem. I'm not sure from the README where my template is supposed to be. I'd like it in the view right next to the docx.erb file. Even if I specify an absolute path it's ignored.

  def show
    respond_to do |format|
      format.html
      format.docx do
        render docx: 'show', filename: "#{@person.last_name.downcase}_cv_#{Date.today}", word_template: "show_template.docx"
      end
    end
  end

Saving of the generated document

Hello.

I've got a problem in my current Rails project. When a docx-file is downloaded it also needs to be saved at the some server storage (say, file system). I didn't find a way to do this when I use:

render docx: 'my_view', filename: 'my_file.docx'

I solved this problem by modification of Htmltoword docx renderer. I've added an option save_to and relevant logic to the renderer:

render docx: 'report_view', filename: "report-#{id}.docx", save_to: "storage/reports/report-#{id}.docx"
ActionController::Renderers.add :docx do |filename, options|
  # ...
  save_to = options.delete(:save_to)
  # ...
  document = Htmltoword::Document.create(content, word_template, extras)
  File.open(save_to, "wb") { |out| out << document } if save_to
  send_data document, filename: file_name, type: Mime::DOCX, disposition: disposition
end

But then I thought what if a developer needs more powerful control over the document. For example, if he wants to save the document to the database? Then, I thought, the most correct way is to give the user the generated document and let him decide what to do. So I ended up with an idea of hooks (or callbacks):

hook = save_document_to_database
Htmltoword::RendererHooks.execute_with_hook (hook) do |hook_id|
  render docx: 'my_view', filename: 'my_file.docx', hook: hook_id
end

execute_with_hook assigns the given hook with an ID and registers it in the static hash, passes the ID to the block and unregister the hook after block's completion. Docx renderer checks if there is a hook option and calls it after a document is generated.

Moreover, it's possible to generalize the idea and create multiple hooks: document_created, document_sent etc with usage like this:

hooks = {document_created: save_document_to_database, document_sent: log_sent_time} 
Htmltoword::RendererHooks.execute_with_hook (hooks) do |hook_ids|
  render docx: 'my_view', filename: 'my_file.docx', hooks: hook_ids
end

How do you think: which option is the best and is it worth doing at all? I've got the first two options already working, so if you approve any of it I can commit.

Nested nume

Now generating

  1. xxx
  2. xxx
  3. xxx
    1. xxx
  4. xxx
    1. xxx
    2. xxx
  5. xxx

Need something like that:

  1. xxx
    1.1. xxx
    1.1.1. xxx
    1.1.2. xxx
    1.2. xxx
    1.2.1 xxx
    1.2.2 xxx
  2. xxx
    2.1. xxx

Can somebody explain how to change of xslt file?

Any mechanism to do table column width?

I'd like to have a table where the first column is always a specific width. I've discovered that "Table Styles" in word do not have this functionality. (Font styles and borders and everything else I need are).

I've tried all sorts of html / css things in the hope Word would parse it without success. Is there a trick to pass a column width? Something along the lines of the highlight mechanism listed in the README?

Thanks!

Add TOC

Is there a way to force the table of contents to be generated on the word doc? I cannot find any examples and there is no way that I am seeing by the various things that I have tried.

Does not work on JRuby: could not parse xslt stylesheet

Trying to use the htmltoword gem, v. 0.4.4, to generate a Word document from an HTML string on JRuby 1.7.21, Oracle JDK 1.8.0_45, and Nokogiri gem 1.6.6.2.

module Integrator
  module Export
    class Docx
      require 'htmltoword'

      def export
        Htmltoword::Document.create('<html><head></head><body><p>Hello</p></body></html>')
      end

    end
  end
end

It fails when trying to parse the XSLT stylesheet numbering.xslt, with the unhelpful error message

RuntimeError (could not parse xslt stylesheet):
  lib/integrator/export/docx.rb:7:in `export'

It is actually failing on the first line of Htmltoword::Document::transform_and_replace when trying to open the file with Nokogiri. When running on JRuby, Nokogiri uses Apache Xerces to parse XSL files rather than libxslt, and from what I understand, libxslt is more forgiving about bad stylesheets.

I'm lacking any good XSLT editors. I did try to run this in Eclipse and received the following error there:

Cannot convert data-type 'void' to 'reference'.
    at com.sun.org.apache.xalan.internal.xsltc.compiler.CastExpr.typeCheck(CastExpr.java:188)
    at com.sun.org.apache.xalan.internal.xsltc.compiler.CastExpr.<init>(CastExpr.java:143)
    at com.sun.org.apache.xalan.internal.xsltc.compiler.WithParam.typeCheck(WithParam.java:146)
    at com.sun.org.apache.xalan.internal.xsltc.compiler.SyntaxTreeNode.typeCheckContents(SyntaxTreeNode.java:493)
    at com.sun.org.apache.xalan.internal.xsltc.compiler.CallTemplate.typeCheck(CallTemplate.java:98)
    at com.sun.org.apache.xalan.internal.xsltc.compiler.SyntaxTreeNode.typeCheckContents(SyntaxTreeNode.java:493)
    at com.sun.org.apache.xalan.internal.xsltc.compiler.LiteralElement.typeCheck(LiteralElement.java:201)
    at com.sun.org.apache.xalan.internal.xsltc.compiler.SyntaxTreeNode.typeCheckContents(SyntaxTreeNode.java:493)
    at com.sun.org.apache.xalan.internal.xsltc.compiler.When.typeCheck(When.java:93)
    at com.sun.org.apache.xalan.internal.xsltc.compiler.SyntaxTreeNode.typeCheckContents(SyntaxTreeNode.java:493)
    at com.sun.org.apache.xalan.internal.xsltc.compiler.Instruction.typeCheck(Instruction.java:41)
    at com.sun.org.apache.xalan.internal.xsltc.compiler.SyntaxTreeNode.typeCheckContents(SyntaxTreeNode.java:493)
    at com.sun.org.apache.xalan.internal.xsltc.compiler.Template.typeCheck(Template.java:295)
    at com.sun.org.apache.xalan.internal.xsltc.compiler.SyntaxTreeNode.typeCheckContents(SyntaxTreeNode.java:493)
    at com.sun.org.apache.xalan.internal.xsltc.compiler.Stylesheet.typeCheck(Stylesheet.java:657)
    at com.sun.org.apache.xalan.internal.xsltc.compiler.Parser.createAST(Parser.java:411)
    at com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC.compile(XSLTC.java:486)
    at com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC.compile(XSLTC.java:571)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:977)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:791)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTransformerHandler(TransformerFactoryImpl.java:1099)
    at org.eclipse.wst.xsl.jaxp.debug.invoker.internal.JAXPSAXProcessorInvoker.addStylesheet(JAXPSAXProcessorInvoker.java:137)
    at org.eclipse.wst.xsl.jaxp.debug.invoker.internal.JAXPSAXProcessorInvoker.addStylesheet(JAXPSAXProcessorInvoker.java:128)
    at org.eclipse.wst.xsl.jaxp.debug.invoker.PipelineDefinition.addStyleSheet(PipelineDefinition.java:161)
    at org.eclipse.wst.xsl.jaxp.debug.invoker.PipelineDefinition.configure(PipelineDefinition.java:152)
    at org.eclipse.wst.xsl.jaxp.debug.invoker.internal.Main.main(Main.java:72)
14:46:58,596 FATAL [main] Main  - Error with stylesheet: file:/Users/snelson/workspaces/xslt/numbering.xslt
org.eclipse.wst.xsl.jaxp.debug.invoker.internal.ConfigurationException: Error with stylesheet: file:/Users/snelson/workspaces/xslt/numbering.xslt
    at org.eclipse.wst.xsl.jaxp.debug.invoker.PipelineDefinition.addStyleSheet(PipelineDefinition.java:163)
    at org.eclipse.wst.xsl.jaxp.debug.invoker.PipelineDefinition.configure(PipelineDefinition.java:152)
    at org.eclipse.wst.xsl.jaxp.debug.invoker.internal.Main.main(Main.java:72)

But I'm still not sure exactly where the problem lies.

Сan't convert large file (~100k rows in HTML)

When I try to convert a large file (~100k rows in HTML), I get an error:
stack level too deep
my code is:

respond_to do |format|
  format.docx do
    render docx: 'report', filename: "report.docx"
  end
end

Small files are generated perfectly.

If I change the code to this:

respond_to do |format|
  format.docx do
    html = render_to_string template: 'report', formats: :docx
    result = Htmltoword::Document.create(html)
    send_data result, type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document", filename: "report.docx"
  end
end

then the rails server is stopped with error:

/Users/developer/.rvm/gems/ruby-2.5.1@plutus/gems/htmltoword-1.1.0/lib/htmltoword/document.rb:104: [BUG] Illegal instruction at 0x00007fff7164f5e2
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17]

-- Crash Report log information --------------------------------------------
   See Crash Report log file under the one of following:                    
     * ~/Library/Logs/DiagnosticReports                                     
     * /Library/Logs/DiagnosticReports                                      
   for more details.                                                        
Don't forget to include the above Crash Report log file in bug reports.     

-- Control frame information -----------------------------------------------
c:0112 p:---- s:0851 e:000850 CFUNC  :transform
...

I tried using different versions of rails and ruby, but this does not work. Other gems, such as pandoc-ruby, convert such large files, but text formatting does not work in them.

Default template is in Danish

The default template is set to Danish (which took me a bit to figure out). Is it possible to tell the default template to respect the system default language (rather than hard coding one specific one)?

List item bullets corrupted in Microsoft Word

It seems list item bullets render as corrupt characters in Microsoft Word. No issue in other software we tried (Pages, Preview, ...).

require "htmltoword"

html = "<ul><li>Line 1</li><li>Line 2</li></ul>"
file = Htmltoword::Document.create_and_save(html, "test.docx")

Result:

  • gem version htmltoword-1.0.0
  • Microsoft Word for Mac 15.26 (160910)
  • Also received complaints from other people where the item bullet was rendered as ∉ (possibly on Windows this time)

After an initial comparison of the xml files of the corrupted list versus of a working list, it seems removing these lines:

      <w:rPr>
        <w:rFonts w:ascii="Symbol" w:hAnsi="Symbol" w:hint="default"/>
      </w:rPr>

from the numbering.xml file solves this problem (but might cause others, I'm not sure what these lines do), which might give a hint towards how to solve this.

Use a template from another directory

Hello

I'm using the version of htmltoword 0.5.1

My controller code

class ReportsController < ApplicationController
  respond_to :docx
  def run
    @report = Report.find(params[:id])
  end

  def execute
    @report = Report.find(params[:id])
    @params = params
    @user = current_user
    if @report.format == 'html'
      render @report.template.path
    elsif @report.format == 'docx'
      file_name = @report.name + '.' + @report.format
      render docx: @report.template.path, filename: file_name
    elsif @report.format == 'xlsx'
      file_name = @report.name + '.' + @report.format
      render file: @report.template.path
    end
  end
end

Reports are created and their templates are downloaded with carrierwave to the folder
/uploads/report_templates/13/REPORT_NAME

But if I use the code I wrote above, then the application gives an error 404.
Tell me how it can be fixed or bypassed?

Xml parsing error

Hi, I'm trying to get Word generation working with v0.7.0 but the docx file generated always seems to be corrupt. I get the same error every time when trying to open the document in Word:

Xml parsing error
Location: Part: /word/document.xml, Line: 14, Column 6

I've unpacked the docx and examined the file and it seems fine to my uneducated eye. This is line 14:

<w:r xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">

I've tried opening in Word on both Mac and Windows with the same result.

I was initially trying this with an existing rails 5.1 app but when I hit this error I tried on clean installations of rails 5.1 and 4.2 with the same result. I based my controller action on the most basic examples from the docs too but still no joy:

respond_to :docx
def show
  respond_to do |format|
    format.docx do
      render docx: 'my_file.docx', content: '<html><body>some html</body></html>'
    end
  end
end

Any ideas?

Space inside elements

If I convert the HTML bellow, I get two blank lines between ______ and the user name:

___________________________________
<%= @user.name %>

I think the problem is that the gem doesn't remove the "\n" from the HTML file.

Can't render images on the document

I'm trying to render images on the docx, but with no success. I even tried with the example in the readme and the images just dont appear.

The image html i'm trying to render is:
<p style='text-align: center;'><img src=\"http://placehold.it/250x100.png\" style=\"width: 250px; height: 100px\"></p>\n ... rest of the html.

using this call in the controller:
render docx: "document.docx", content: document_html

I'm using the 1.1.0 version of the gem, with 5.0.2 rails version.

Using classes for formatting

Hi All,

This is a really remarkable gem that you folks have been working on; I really haven't found anything so complete for dealing with docx formatting. Classes seem to be the key mechanism for customization but I can't find a list of accepted classes for this gem. Where might I find a general list of accepted formatting classes/options?

How Paste image to docx

I try paste image

@thumb = '<img src="http://www.fips.ru/rutmimage/0/200000/270000/270000/270041-s.JPG" border="0">'.to_html

as raw (this is HAML)
= raw @thumb
then i get error

Unrecognized unit of measure: . Unrecognized unit of measure: . Unrecognized unit of measure: . Unrecognized unit of measure: .

Haw can i paste image from URI fo example?
Help please!

Showing images in generated document

Hello,
I have a file myfile.docx.haml with images in it. = image_tag 'logo'
But the images are not present in the generated doc from this file.
How can i generate a doc with images in it ?

Can we apply CSS?

Is it possible to apply cs to docs file using this gem?
I tried using classes to change the colors of the text. But I am unable to do so. Can you help me out here?

Support for Merged Cell in Tables

I have an html to be converted to word doc:

<table align="center" border="1" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td colspan="2">Lorem Ipsum with Colspan 2</td>
</tr>
<tr>
<td><strong>Lorem Ipsum</strong></td>
<td><strong>Lorem Ipsum</strong></td>
</tr>
</tbody>
</table>

It suppose to look like this:
screen shot 2016-10-21 at 3 01 09 pm

But in my document it appears to be like this:
screen shot 2016-10-21 at 2 19 58 pm

Does anyone have a solution for this?

ActionController::UnknownFormat

Hi,

I am unable to view the docx file, as every time I go and try accessing the routes, it throws error

ActionController::UnknownFormat (ActionController::UnknownFormat):
app/controllers/enterprisecycles_controller.rb:22:in `testcycle_requirement_document'

respond_to do |format|
format.docx do

I have written following code:

respond_to :docx

def testcycle_requirement_document
  respond_to do |format|
    format.docx do
      render docx: 'requirement_document', filename: 'my_file.docx'
    end
  end
end

I am using htmltoword (0.5.1)

Kindly help.

respond_with not taking arguments?

I'm having trouble getting all the pieces working.
I have a template in /lib/htmltoword/templates/mytemplate.ott
I installed the gem, put it in the gem file, bundle install
at the top of my controller: respond_to :docx

in my controller function I've tried a zillion things at this point. but it seems respond_with doesn't accept arguments at all

respond_with(@project, filename: 'meh.docx', word_template: 'mytemplate.ott')

gives me a file based off of @project and the function_name.docx.erb file in the views, but the file name is 'function_name.docx', and no sign of the template at all. completely equivalent response when I replace the line with

respond_with(@project)

I tried replacing with:

render docx: 'function_name', filename: 'foo.docx', word_template: 'mytemplate.ott'

and I get the file name right, still no sign of the template, and the bgcolor tags I have in the .docx.erb file no longer show up. in addition, the font size changed from 12 to 9, but the template sets them to 10, so it's still not getting the right template, though it is getting a different one(or maybe just a more default default).

When I look at it, it says user error to me, but I'm really stuck >_< I appreciate any direction you could give me.

Thanks

Change xsltMaxDepth (--maxdepth) value? - where?

Hi, I'm using Rails 3.2.12 with your Gem to create a big docx file containing the site's users with their details.

It works great with a few users, but when there are more than a few I'm getting the following error:

xsltApplyXSLTTemplate: A potential infinite template recursion was detected.
You can adjust xsltMaxDepth (--maxdepth) in order to raise the maximum number of nested template calls and variables/params (currently set to 3000).

I'm not familiar with xslt, I didn't find where I should add the maxdepth parameter-
Any ideas?

Thank you.

Full error, and what I'm trying to do below:

RuntimeError: runtime error: file C:/row/Ruby21/lib/ruby/gems/2.1.0/gems/htmltoword-0.5.1/lib/htmltoword/xslt/inline_elements.xslt line 7 element copy
xsltApplyXSLTTemplate: A potential infinite template recursion was detected.
You can adjust xsltMaxDepth (--maxdepth) in order to raise the maximum number of nested template calls and variables/params (currently set to 3000).
Templates:
#0 name node()|@*
#1 name node()|@*
#2 name node()|@*
#3 name node()|@*
#4 name node()|@*
#5 name node()|@*
#6 name node()|@*
#7 name node()|@*
#8 name node()|@*
#9 name node()|@*
#10 name node()|@*
#11 name node()|@*
#12 name node()|@*
#13 name node()|@*
#14 name node()|@*
Variables:
from C:/row/Ruby21/lib/ruby/gems/2.1.0/gems/htmltoword-0.5.1/lib/htmltoword/document.rb:88:in `transform'

More details:

I'm trying to send a mail with the docx file as attachment (It does work, but not with many users):

class MyMailer < ActionMailer::Base
  default from: "[email protected]"
  default to: "[email protected]"

  def send_word_files(users)
    body = ''
    users.each do |user|
      user.fields.each do |field|
        
        body << "<h2>#{field.label_name}:</h2>"
        body << "</p>#{user.field_value(field)}</p>"
      end
      body << '<div class="-page-break"></div>'
    end
    attachments["users.docx"] = Htmltoword::Document.create("<html><head></head><body>#{body}</body></html>")
  
    mail(subject: "word file", body: 'enjoy')
  end

end

create_and_save not found

In 0.4.2 I don't have the create_and_save method

2.2.1 :009 > Htmltoword::Document.methods.select { |m| m=~ /create/ }
=> [:create, :create_with_content]

Do I need to track master? The document implies this is available in 0.4+

"[Invalid encoding]"

When I try to Htmltoword::Document.create(content, word_template, extras), I got result "[Invalid encoding]". I went through generate method and I have found out that 4 parts of template (docProps/thumbnail.jpeg, word/fontTable.xml, word/numbering.xml, word/theme/theme1.xml) have encoding ASCII-8BIT and the rest have UTF-8.

I don't know, if this is the problem, because I tried .force_encoding('utf-8') on every part when generating with no result.

Issues with nested lists

I'm looking for a solution to convert HTML to WordprocessingML. I found this gem which looks like an awesome foundation for what I'm building (eventually I need to insert many html fragments into a template).

While running some tests to verify the output I noticed that there are some issues with nested lists. I put my test data in this Gist. The output looks like this:

screen shot 2015-12-16 at 12 19 23

Looks like there are two distinct issues:

  1. Additional indent
  2. Mixed lists (bullets in numbers or numbers in bullets) are not recognized and get displayed like the outer list.

The additional indent seems to be related to whitespace in the input HTML.It happens with HTML that looks like:

<ul>
  <li>lorem ipsum</li>
  <li>
    consectetur adipiscing elit
    <ul>

But doesn't happen with HTML that looks like this:

<ul>
  <li>lorem ipsum</li>
  <li>consectetur adipiscing elit
  <ul>

Are these use-cases that you are looking to support? I'm more than happy to provide additional information if something is missing.

Retreive WordProcessingML After Conversion?

Hello,

I need to convert some HTML and use the corresponding WordProcessingML in another script. Is there a way for me to get the WordProcessingML after converting my HTML content? So, instead of Htmltoword::Document.create(html) returning a ".docx" file, is there an option or another function that returns the WordProcessingML? I've looked through the code but don't see anything that fits what I need.

Thanks!

rails5.1.2 uninitialized constant Mime::DOCX

when I use rails5.1.2,
NameError (uninitialized constant Mime::DOCX):
respond_to :docx
def word
respond_to do |format|
format.docx do
render docx: 'word', filename: 'word.docx'
end
end
end

Gemfile
gem 'responders'
gem 'htmltoword'

gem 'rails', '~> 5.1.2'

routes
get "word" => "homes#word", format: 'docx'

Bug in rails 5

Hey I'm trying to use this gem with my rails project, i'm already following all your tutor but i when i change template to .docx.erb it not downloaded and in log development it keep showing DEPRECATION WARNING: Accessing mime types via constants is deprecated. Please change Mime::DOCX to Mime[:docx] when i use format.docx please help

Change default font type and font size

How can I change font size and font type? I am losing my mind, whatever I do, it is always Arial 9pt.
I tried
body { font-family: "Times New Roman", Times, serif; font-size:16px; font-weight:600 }

`
<w:p>
<w:r>
<w:rPr>
<w:sz w:val="50"/>
<w:rFonts w:ascii="Times New Roman" />
</w:rPr>
<w:t xml:space="preserve">This paragraph is Times New Roman at 25pt</w:t>
</w:r>
</w:p>

Some text

` And nothing works :(

In Headings not text-align.

How to text-align an heading?

Whatever from h1 to h6 nothing to do with text-align:center, right, left or justify.

It works with p tag.

How to?

Spaces removed

Hi,

I'm having an issue when i try to convert my html.
For example:
<show><test>Hello``</test><test><strong>World</strong>``</test><test>RoR!</test></show>
The HTML is:

Hello World RoR!

There is a space between World and RoR!

But the result in Docx is:

Hello WorldRoR!

Of course strong tag is juste 1 case same isue with , etc...

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.