装逼代码html,装逼匠/html-to-word

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

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 = '

Hello

'

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 = '

Hello

'

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:'

some 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:'

some html'

end

end

end

Example of my_view.docx.erb

My custom template

Example of _my_partial.docx.erb

My html for item goes here

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('\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:

This text will have a green highlight

Page breaks

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

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:

Transform inputed html to wordml.

Unzip empty word docx file bundled with gem and replace its document.xml content with the new transformed result of step 1.

Zip up contents again into a resulting .docx file.

Contributions would be very much appreciated.

Fork it

Create your feature branch (git checkout -b my-new-feature)

Commit your changes (git commit -am 'Add some feature')

Push to the branch (git push origin my-new-feature)

Create new Pull Request

License

(The MIT License)

Copyright © 2013:

Cristina Matonte

Nicholas Frandsen

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值