MsOffice-Word-HTML-Writer-1.07000755000000000000 014203002615 16401 5ustar00unknownunknown000000000000MsOffice-Word-HTML-Writer-1.07/Build.PL000444000000000000 151614203002061 20030 0ustar00unknownunknown000000000000use strict; use warnings; use Module::Build; my $builder = Module::Build->new( module_name => 'MsOffice::Word::HTML::Writer', license => 'perl', dist_author => 'Laurent Dami ', dist_version_from => 'lib/MsOffice/Word/HTML/Writer.pm', requires => { 'utf8' => 0, 'Test::More' => 0, 'Carp' => 0, 'MIME::Base64' => 0, 'MIME::Types' => 0, 'Module::Build' => 0, 'Params::Validate' => 0, 'Scalar::Util' => 0, }, add_to_cleanup => [ 'MsOffice-Word-HTML-Writer-*' ], meta_merge => { resources => { repository => 'https://github.com/damil/MsOffice-Word-HTML-Writer', } }, ); $builder->create_build_script(); MsOffice-Word-HTML-Writer-1.07/Changes000444000000000000 255114203000147 20032 0ustar00unknownunknown000000000000Revision history for MsOffice-Word-HTML-Writer 1.07 15.02.2022 - fix bug introduced in v1.04 : wide chars should be encoded as HTML numeric entities 1.06 30.01.2022 - fix bug introduced in v1.04 : images should not be utf8-encoded (thanks Rob. Wohlfarth!) 1.05 21.02.2021 - fix RT #107457 and #104205; wrong behaviour and wrong documentation about page breaks 1.04 11.02.2020 - support for UTF8 contents 1.03 05.04.2013 - new argument 'prevent_html_encoding' for the quote() method - save_as() method returns the generated filename 1.02 23.11.2013 - #90737 : new 'page_numbers' arg for create_section(new_page=>) - avoid warnings when the MIME type is unknown 1.01 29.09.2011 - new 'left'/'right' args for page_break() and create_section(new_page=>) 1.00 13.08.2011 - doc updates - bumped to 1.0 and removed the claim that it was "experimental" 0.07 06.01.2011 - no "soft line breaks" in MIME encode_qp() -- see RT 63716 0.06 28.07.2010 - new arg for new(): WordDocument properties 0.05 14.05.2010 - better handling of MsWord fields, and new method quote() 0.04 04.06.2009 - fixes on CRLF line endings when generating from Unix 0.03 05.01.2009 - forgot Mime::Types in prereq 0.02 01.01.2009 - Oops, already some bugs (bad calls to Params::Validate) 0.01 01.01.2009 - Initial release MsOffice-Word-HTML-Writer-1.07/MANIFEST000444000000000000 27114202777746 17676 0ustar00unknownunknown000000000000Build.PL Changes lib/MsOffice/Word/HTML/Writer.pm MANIFEST This list of files META.yml README t/00-load.t t/01_word_document.t t/02_latin1.t t/pod.t Makefile.PL META.json MsOffice-Word-HTML-Writer-1.07/META.json000444000000000000 250714203002615 20163 0ustar00unknownunknown000000000000{ "abstract" : "Writing documents for MsWord in HTML format", "author" : [ "Laurent Dami " ], "dynamic_config" : 1, "generated_by" : "Module::Build version 0.4231", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : 2 }, "name" : "MsOffice-Word-HTML-Writer", "prereqs" : { "configure" : { "requires" : { "Module::Build" : "0.42" } }, "runtime" : { "requires" : { "Carp" : "0", "MIME::Base64" : "0", "MIME::Types" : "0", "Module::Build" : "0", "Params::Validate" : "0", "Scalar::Util" : "0", "Test::More" : "0", "utf8" : "0" } } }, "provides" : { "MsOffice::Word::HTML::Writer" : { "file" : "lib/MsOffice/Word/HTML/Writer.pm", "version" : "1.07" } }, "release_status" : "stable", "resources" : { "license" : [ "http://dev.perl.org/licenses/" ], "repository" : { "url" : "https://github.com/damil/MsOffice-Word-HTML-Writer" } }, "version" : "1.07", "x_serialization_backend" : "JSON::PP version 4.05" } MsOffice-Word-HTML-Writer-1.07/META.yml000444000000000000 157214203002615 20014 0ustar00unknownunknown000000000000--- abstract: 'Writing documents for MsWord in HTML format' author: - 'Laurent Dami ' build_requires: {} configure_requires: Module::Build: '0.42' dynamic_config: 1 generated_by: 'Module::Build version 0.4231, CPAN::Meta::Converter version 2.150010' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: MsOffice-Word-HTML-Writer provides: MsOffice::Word::HTML::Writer: file: lib/MsOffice/Word/HTML/Writer.pm version: '1.07' requires: Carp: '0' MIME::Base64: '0' MIME::Types: '0' Module::Build: '0' Params::Validate: '0' Scalar::Util: '0' Test::More: '0' utf8: '0' resources: license: http://dev.perl.org/licenses/ repository: https://github.com/damil/MsOffice-Word-HTML-Writer version: '1.07' x_serialization_backend: 'CPAN::Meta::YAML version 0.018' MsOffice-Word-HTML-Writer-1.07/Makefile.PL000444000000000000 133714175436257 20541 0ustar00unknownunknown000000000000# Note: this file was auto-generated by Module::Build::Compat version 0.4231 use ExtUtils::MakeMaker; WriteMakefile ( 'NAME' => 'MsOffice::Word::HTML::Writer', 'VERSION_FROM' => 'lib/MsOffice/Word/HTML/Writer.pm', 'PREREQ_PM' => { 'Carp' => 0, 'Encode' => 0, 'MIME::Base64' => 0, 'MIME::QuotedPrint' => 0, 'MIME::Types' => 0, 'Module::Build' => 0, 'Params::Validate' => 0, 'Scalar::Util' => 0, 'Test::More' => 0, 'utf8' => 0 }, 'INSTALLDIRS' => 'site', 'EXE_FILES' => [], 'PL_FILES' => {} ) ; MsOffice-Word-HTML-Writer-1.07/README000444000000000000 461611711652770 17443 0ustar00unknownunknown000000000000MsOffice-Word-HTML-Writer MsOffice::Word::HTML::Writer - Writing documents for MsWord in HTML format The present module is one way to programatically generate documents targeted for Microsoft Word (MsWord). MsWord can read documents encoded in native binary format, in Rich Text Format (RTF), in WordML (an XML dialect), or -- maybe this is less known -- in HTML, with some special markup for pagination and other MsWord-specific features. Such HTML documents are often in several parts, because attachments like images or headers/footers need to be in separate files; however, since it is more convenient to carry all data in a single file, MsWord also supports the "MHTML" format (or "MHT" for short), i.e. an encapsulation of a whole HTML tree into a single file encoded in MIME multipart format. This format can be generated interactively from MsWord by calling the "SaveAs" menu and choosing the F<.mht> extension. C helps you to programatically generate MsWord documents in MHT format. The advantage of this technique is that one can rely on standard HTML mechanisms for layout control, such as styles, tables, divs, etc -- of course this markup can be produced using your favorite HTML module. The added value of C is to help building the MIME multipart file, and provide some abstractions for representing MsWord-specific features (headers, footers, fields, etc.). This is probably the most convenient way for programmatic document generation. INSTALLATION To install this module, run the following commands: perl Makefile.PL make make test make install SUPPORT AND DOCUMENTATION After installing, you can find documentation for this module with the perldoc command. perldoc MsOffice::Word::HTML::Writer You can also look for information at: Search CPAN http://search.cpan.org/dist/MsOffice-Word-HTML-Writer CPAN Request Tracker: http://rt.cpan.org/NoAuth/Bugs.html?Dist=MsOffice-Word-HTML-Writer AnnoCPAN, annotated CPAN documentation: http://annocpan.org/dist/MsOffice-Word-HTML-Writer CPAN Ratings: http://cpanratings.perl.org/d/MsOffice-Word-HTML-Writer COPYRIGHT AND LICENCE Copyright (C) 2008 Laurent Dami This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. MsOffice-Word-HTML-Writer-1.07/lib000755000000000000 014203002615 17147 5ustar00unknownunknown000000000000MsOffice-Word-HTML-Writer-1.07/lib/MsOffice000755000000000000 014203002615 20642 5ustar00unknownunknown000000000000MsOffice-Word-HTML-Writer-1.07/lib/MsOffice/Word000755000000000000 014203002615 21555 5ustar00unknownunknown000000000000MsOffice-Word-HTML-Writer-1.07/lib/MsOffice/Word/HTML000755000000000000 014203002615 22321 5ustar00unknownunknown000000000000MsOffice-Word-HTML-Writer-1.07/lib/MsOffice/Word/HTML/Writer.pm000444000000000000 7726414203001617 24331 0ustar00unknownunknown000000000000package MsOffice::Word::HTML::Writer; use warnings; use strict; use MIME::Base64 qw/encode_base64/; use MIME::Types; use Carp; use Params::Validate qw/validate SCALAR HASHREF/; use Scalar::Util qw/looks_like_number/; our $VERSION = '1.07'; sub new { my $class = shift; # validate named parameters my $param_spec = { title => {type => SCALAR, optional => 1}, head => {type => SCALAR, optional => 1}, hf_head => {type => SCALAR, optional => 1}, WordDocument => {type => HASHREF, optional => 1}, }; my %params = validate(@_, $param_spec); # create instance my $self = { MIME_parts => [], sections => [{}], title => $params{title} || "Document generated by MsOffice::Word::HTML::Writer", head => $params{head} || "", hf_head => $params{hf_head} || "", WordDocument => $params{WordDocument}, }; bless $self, $class; } sub create_section { my $self = shift; # validate named parameters my $param_spec = {page => {type => HASHREF, optional => 1}}; $param_spec->{$_} = {type => SCALAR, optional => 1} for qw/header footer first_header first_footer new_page/; my %params = validate(@_, $param_spec); # if first automatic section is empty, delete it $self->{sections} = [] if scalar(@{$self->{sections}}) == 1 && !$self->{sections}[0]{content}; # add the new section push @{$self->{sections}}, \%params; } sub write { my $self = shift; # add html arguments to current section content $self->{sections}[-1]{content} .= join ("", @_); } sub save_as { my ($self, $filename) = @_; # default extension is ".doc" $filename .= ".doc" unless $filename =~ /\.\w{1,5}$/; # open the file open my $fh, ">:crlf", $filename or croak "could not open >$filename: $!"; # write content and close print $fh $self->content; close $fh; return $filename; } sub attach { my ($self, $name, $open1, $open2, @other) = @_; # open a handle to the attachment (need to dispatch according to number # of args, because perlfunc/open() has complex prototyping behaviour) my $fh; if (@other) { open $fh, $open1, $open2, @other or croak "open $open1, $open2, @other : $!"; } elsif ($open2) { open $fh, $open1, $open2 or croak "open $open1, $open2 : $!"; } else { open $fh, $open1 or croak "open $open1 : $!"; } # slurp the content binmode($fh) unless $name =~ /\.(html?|css|te?xt|rtf)$/i; local $/; my $attachment = <$fh>; # add the attachment (filename and content) push @{$self->{MIME_parts}}, ["files/$name", $attachment]; } sub page_break { my ($self) = @_; return qq{
\n}; } sub tab { my ($self, $n_tabs) = @_; $n_tabs ||= 1; return qq{}; } sub field { my ($self, $fieldname, $args, $content, $prevent_html_entity_encoding) = @_; for ($args, $content) { $_ ||= ""; # undef replaced by empty string s/&/&/g, s//>/g # replace HTML entities unless $prevent_html_entity_encoding; } my $field; # when args : long form of field encoding if ($args) { my $space = qq{ }; $field = qq{} . $space . $fieldname . $space . $args . qq{} . $content . qq{}; } # otherwise : short form of field encoding else { $field = qq{$content}; } return $field; } sub quote { my ($self, $text, $prevent_html_entity_encoding) = @_; my $args = $text; $args =~ s/"/\\"/g; $args = qq{"$args"}; $args =~ s/"/"/g; return $self->field('QUOTE', $args, $text, $prevent_html_entity_encoding); } sub content { my ($self) = @_; # separator for parts in MIME document my $boundary = qw/__NEXT_PART__/; # MIME multipart header my $mime = qq{MIME-Version: 1.0\n} . qq{Content-Type: multipart/related; boundary="$boundary"\n\n} . qq{MIME document generated by MsOffice::Word::HTML::Writer\n\n}; # generate each part (main document must be first) my @parts = $self->_MIME_parts; my $filelist = $self->_filelist(@parts); for my $pair ($self->_main, @parts, $filelist) { my ($filename, $content) = @$pair; my $mime_type = MIME::Types->new->mimeTypeOf($filename) || ''; my ($encoding, $encoded); if ($mime_type =~ /^text|xml$/) { # no need for Windows-style end-of-lines of shape CRLF $content =~ s/\r\n/\n/g; # multibyte chars are encoded as numerical HTML entities $content =~ s/([^\x{0}-\x{FF}])/'&#'.ord($1).';'/eg; # simple-minded MIME quoted-printable encoding, using latin-1 encoding $encoding = 'quoted-printable'; ($encoded = $content) =~ s/=/=3D/g; $mime_type .= "; charset=iso-8859-1"; } else { $encoding = 'base64'; $encoded = encode_base64($content); } $mime .= qq{--$boundary\n} . qq{Content-Location: file:///C:/foo/$filename\n} . qq{Content-Transfer-Encoding: $encoding\n} . qq{Content-Type: $mime_type\n\n} . $encoded . "\n"; } # close last MIME part $mime .= "--$boundary--\n"; return $mime; } #====================================================================== # PRIVATE METHODS #====================================================================== sub _main { my ($self) = @_; # body : concatenate content from all sections my $body = ""; my $i = 1; foreach my $section (@{$self->{sections}}) { # section break if ($i > 1) { # type of break my $break = $section->{new_page}; $break = 'always' if $break && looks_like_number($break); # if true but not a word $break ||= 'auto'; # if false # otherwise, type of break will just be the word given in {new_page} # insert into body my $style = qq{page-break-before:$break;mso-break-type:section-break}; $body .= qq{
\n}; } # section content $body .= qq{
\n$section->{content}\n
\n}; $i += 1; } # assemble head and body into a full document my $html = qq{\n} . $self->_head . qq{\n$body\n} . qq{\n}; return ["main.htm", $html]; } sub _head { my ($self) = @_; # HTML head : link to filelist, title, view format and styles my $head = qq{\n} . qq{\n} . qq{$self->{title}\n} . $self->_xml_WordDocument . qq{\n} . $self->{head} . qq{\n}; return $head; } sub _xml_WordDocument { my ($self) = @_; my $xml_root = $self->{WordDocument} or return ""; return "\n" . _w_xml($xml_root) . "\n"; } sub _w_xml { my $node = shift; my $xml = ""; while (my ($k, $v) = each %$node) { $xml .= $v ? ( # node with content "" . (ref $v ? _w_xml($v) : $v) . "\n" ) : "\n"; # node without content } return $xml; } sub _section_styles { my ($self) = @_; my $styles = ""; my $i = 1; foreach my $section (@{$self->{sections}}) { my $properties = ""; # page properties (size and margin) foreach my $prop (qw/size margin/) { my $val = $section->{page}{$prop} or next; $properties .= qq{ $prop:$val;\n}; } # headers and footers my $has_first_page; foreach my $prop (qw/header_margin footer_margin page_numbers paper_source/) { my $val = $section->{page}{$prop} or next; (my $property = $prop) =~ s/_/-/g; $properties .= qq{ mso-$property:$val;\n}; } foreach my $hf (qw/header footer first_header first_footer/) { $section->{$hf} or next; $has_first_page = 1 if $hf =~ /^first/; (my $property = $hf) =~ s/_/-/; $properties .= qq{ mso-$property:url("files/header_footer.htm") $hf$i;\n}; } $properties .= qq{ mso-title-page:yes;\n} if $has_first_page; # style definitions for this section $styles .= qq[\@page Section$i {\n$properties}\n] . qq[div.Section$i {page:Section$i}\n]; $i += 1; } return $styles; } sub _MIME_parts { my ($self) = @_; # attachments supplied by user my @parts = @{$self->{MIME_parts}}; # additional attachment : computed file with headers and footers my $hf_content = $self->_header_footer; unshift @parts, ["files/header_footer.htm", $hf_content] if $hf_content; return @parts; } sub _header_footer { my ($self) = @_; # create a div for each header/footer in each section my $hf_divs = ""; my $i = 1; foreach my $section (@{$self->{sections}}) { # deal with headers/footers defined in that section foreach my $hf (qw/header footer first_header first_footer/) { $section->{$hf} or next; (my $style = $hf) =~ s/^first_//; $hf_divs .= qq{
\n} . $section->{$hf} . "\n" . qq{
\n}; } $i += 1; } # if at least one such div, need to create an attached file my $header_footer = !$hf_divs ? "" : qq{\n} . qq{\n} . qq{\n} . $self->{hf_head} . qq{\n} . qq{\n} . $hf_divs . qq{\n} . qq{\n}; return $header_footer; } sub _filelist { my ($self, @parts) = @_; # xml header my $xml = qq{\n} . qq{ \n}; # refer to each attached file foreach my $part (@parts) { $xml .= qq{ \n}; } # the filelist is itself an attached file $xml .= qq{ \n}; # closing tag; $xml .= qq{\n}; return ["files/filelist.xml", $xml]; } 1; __END__ =head1 NAME MsOffice::Word::HTML::Writer - Writing documents for MsWord in HTML format =head1 SYNOPSIS use MsOffice::Word::HTML::Writer; my $doc = MsOffice::Word::HTML::Writer->new( title => "My new doc", WordDocument => {View => 'Print'}, ); $doc->write("

hello, world

", $doc->page_break, "

hello from another page

"); $doc->create_section( page => {size => "21.0cm 29.7cm", margin => "1.2cm 2.4cm 2.3cm 2.4cm", page_numbers => 50, # initial page number within this section }, header => sprintf("Section 2, page %s of %s", $doc->field('PAGE'), $doc->field('NUMPAGES')), footer => sprintf("printed at %s", $doc->field('PRINTDATE')), new_page => 1, # or 'always', or 'left', or 'right' ); $doc->write("this is the second section, look at header/footer"); $doc->attach("my_image.gif", $path_to_my_image); $doc->write(""); my $filename = $doc->save_as("/path/to/some/file"); =head1 DESCRIPTION =head2 Goal The present module is one way to programatically generate documents targeted for Microsoft Word (MsWord). It doesn't need MsWord to be installed, and doesn't even require a Win32 machine (which is why the module is not in the C namespace). =head2 MsWord and HTML MsWord can read documents encoded in old native binary format, in Rich Text Format (RTF), in XML (either ODF or OOXML), or -- maybe this is less known -- in HTML, with some special markup for pagination and other MsWord-specific features. Such HTML documents are often in several parts, because attachments like images or headers/footers need to be in separate files; however, since it is more convenient to carry all data in a single file, MsWord also supports the "MHTML" format (or "MHT" for short), i.e. an encapsulation of a whole HTML tree into a single file encoded in MIME multipart format. This format can be generated interactively from MsWord by calling the "SaveAs" menu and choosing the F<.mht> extension. Documents saved with a F<.mht> extension will not directly reopen in MsWord : when clicking on such documents, Windows chooses Internet Explorer as the default display program. However, these documents can be simply renamed with a F<.doc> extension, and will then open directly in MsWord. By the way, the same can be done with XML or RTF documents. That is to say, MsWord is able to recognize the internal format of a file, without any dependency on the filename. There is one unfortunate restriction, however : when the extension is F<.docx>, MsWord does not accept any internal format different from OOXML. So one has to either stick with the F<.doc> extension, or choose a specific extension like F<.mswhtml> and then associate this extension to the MsWord program : to do so, type the following command in a windows console assoc .mswhtml=Word.Document.12 # for Office 2010, or .8 for Office 2003 =head2 Features of the module C helps you to programatically generate MsWord documents in MHT format. The advantage of this technique is that one can rely on standard HTML mechanisms for layout control, such as styles, tables, divs, etc. Of course this markup can be produced using your favorite HTML templating module; the added value of C is to help building the MIME multipart file, and provide some abstractions for representing MsWord-specific features (headers, footers, fields, etc.). =head2 Advantages of MHT format The MHT format is probably the most convenient way for programmatic document generation, because =over =item * unlike Excel, MsWord native binary format (used in versions up to 2003) is unpublished and therefore cannot be generated without the MsWord executable. =item * remote control of the MsWord program through an OLE connection, as in L, requires a local installation of Microsoft Office, and is not well suited for server-side generation because the MsWord program might hang or might open dialog boxes that require user input. =item * generation of documents in RTF is possible, but authoring the models requires deep knowledge of the RTF structure --- see L. =item * authoring models in XML also requires deep knowledge of the XML structure. Instead of working directly at the XML level, one could use the L distribution on CPAN, which provides programmatic access to the "ODF" XML format used by OpenOffice. MsWord is able to read and produce such ODF files, but this is not fully satisfactory because in that mode many MsWord features are disabled or restricted. The XML format used by MsWord is called "OOXML"; to my knowledge, there is no CPAN module providing an API to this format. =back By contrast, C allows you to produce documents even with little knowledge of MsWord. Besides, since the content is in HTML, it can be assembled with any HTML tool, and therefore also requires little knowledge of Perl. One word of warning, however : opening MHT documents in MsWord is a bit slower than native binary or RTF documents, because MsWord needs to parse the HTML, compute the layout and convert it into its internal representation. Therefore MHT format is not recommended for very large documents. =head2 Usage C is used in production at Geneva courts of law, for generating thousands of documents per day, from hundreds of models, with an architecture of reusable document parts implemented by Template Toolkit mechanisms (macros, blocks and views). =head1 METHODS B : method names that start with a I may change the internal state of the writer object (for example L, L); method names that are I return data without modifying the internal state (for example L, L, L). =head2 new my $doc = MsOffice::Word::HTML::Writer->new(%params); Creates a new writer object. Optional parameters are : =over =item title document title =item head any HTML declarations you may want to include in the C part of the generated document (for example inline CSS styles or links to attached stylesheets). =item hf_head any HTML declarations you may want to include in the C part of the I HTML document (MsWord requires headers and footers to be specified as C
s in a separate HTML document). =item WordDocument a hashref of options to include as an XML island in the HTML C, corresponding to various options in the MsWord "Tools/Options" panel. These will be included in a XML element named C<< >>, and all children elements will be automatically prefixed by C. The hashref may contain nested hashrefs, such as WordDocument => { View => 'Print', Compatibility => {DoNotExpandShiftReturn => "", BreakWrappedTables => ""} } Names and values of options must be found from the Microsoft documentation, or from reverse engineering of HTML files generated by MsWord. =back Parameters may also be passed as a hashref instead of a hash. =head2 write $doc->write("

hello, world

"); Adds some HTML into the document body. =head2 attach $doc->attach($localname, $filename); $doc->attach($localname, "<", \$content); $doc->attach($localname, "<&", $filehandle); Adds an attachment into the document; the attachment will be encoded as a MIME part and will be accessible under C. The remaining arguments to C specify the source of the attachment; they are directly passed to L and therefore have the same API flexibility : you can specify a filename, a reference to a memory variable, a reference to another filehandle, etc. =head2 create_section $doc->create_section( page => {size => "21.0cm 29.7cm", margin => "1.2cm 2.4cm 2.3cm 2.4cm"}, header => sprintf("Section 2, page %s of %s", $doc->field('PAGE'), $doc->field('NUMPAGES')), footer => sprintf("printed at %s", $doc->field('PRINTDATE')), new_page => 1, # or 'always, or 'left', or 'right' ); Opens a new section within the document (or, if this is called before any L, setups pagination parameters for the first section). Subsequent calls to the L method will add content to that section, until the next L call. Pagination parameters are all optional and may be given either as a hash or as a hashref; accepted parameters are : =over =item page Hashref of CSS page styles, such as : =over =item size Paper size (for example C<21cm 29.7cm>) =item margin Margins (top right bottom left). =item header_margin Margin for header =item footer_margin Margin for footer =item page_numbers Initial value for page numbers within this section =item paper_source Parameters for paper source within this section (values for these parameters must be reverse engineered from MsWord HTML output) =back =item header Header content (in HTML) =item first_header Header content for the first page of that section. =item footer Footer content (in HTML). =item first_footer Footer content for the first page. =item new_page If true, a page break will be inserted before the new section. If the argument is the word C<'left'> or C<'right'>, one or two page breaks will be inserted so that the next page is formatted as a left (right) page. If the argument is a numeric true value, it is translated into the word 'always', which tells MsWord to insert a page break in any case. =back =head2 save_as my $filename = $doc->save_as("/path/to/some/file"); Generates the MIME document and saves it at the given location. If no extension is present, file extension F<.doc> will be added by default to the filename; this is returned as the result from the method call. =head2 content Returns the whole MIME-encoded document as a single string; this is used internally by the L method. Direct call is useful if you don't want to save the document into a file, but want to do something else like embedding it in a message or a ZIP file, or returning it as an HTTP response. =head2 page_break $doc->write($doc->page_break); Returns HTML markup for encoding a page break I. Another way of inserting a page break is to create a new section with an C parameter -- see L. =head2 tab my $html = $doc->tab($n_tabs); Returns HTML markup for encoding one or several tabs. If C<$n_tab> is omitted, it defaults to 1. =head2 field my $html = $doc->field($fieldname, $args, $content, $prevent_html_entity_encoding); Returns HTML markup for a MsWord field. Optional C<$args> is a string with arguments or flags for the field. See MsWord help documentation for the list of field names and their associated arguments or flags. Optional C<$content> is the initial displayed content for the field (because unfortunately MsWord does not immediately compute the field content when opening the document; users will have to explicitly request to update all fields, by selecting the whole document and then hitting the F9 key). Optional C<$prevent_html_entity_encoding> is a boolean that prevents the automatic translation of C<< < >>, C<< > >> and C<< & >> characters into HTML entities C<< < >>, C<< > >> and C<< & >>. This is useful if you want to insert some rich text. Here are some examples : my $header = sprintf "%s of %s", $doc->field('PAGE'), $doc->field('NUMPAGES'); my $footer = sprintf "created at %s, printed at %s", doc->field(CREATEDATE => '\\@ "d MM yyyy"'), doc->field(PRINTDATE => '\\@ "dddd d MMMM yyyy" \\* Upper'); my $quoted = $doc->field('QUOTE', '"hello, world"', 'hello, world'); =head2 quote my $html = $doc->quote($text, $prevent_html_entity_encoding); Shortcut to produce a QUOTE field (see last field example just above). The optional C<$prevent_html_entity_encoding> argument is explained in the L method. =head1 AUTHORING MHT DOCUMENTS =head2 HTML for MsWord MsWord does not support the full HTML and CSS standard, so authoring MHT documents requires some trial and error. Basic divs, spans, paragraphs and tables, are reasonably supported, together with their common CSS properties; but fancier features like floats, absolute positioning, etc. may yield some surprises. To specify widths and heights, you will get better results by using CSS properties rather than attributes of the HTML table model. In case of difficulties for implementing specific features, try to see what MsWord does with that feature when saving a document in HTML format (plain HTM, not MHT!). The generated HTML is quite verbose, but after eliminating unnecessary tags one can sometimes figure out which are the key tags (they start with C or C) or the key attributes (they start with C) which correspond to the desired functionality. =head2 Collaboration with the Template Toolkit The L