使用 tex4ebook 生成 epub 和 mobi 格式时出错

使用 tex4ebook 生成 epub 和 mobi 格式时出错

这是我对这里这里。安装文件由@Michal.h21 提供。

当我使用 tex4ebook 并生成 epub 和 mobi 输出时出现错误。

输入文件(sample.tex)是:

\documentclass[ebook,12pt,oneside,openany]{memoir}

\begin{filecontents}{\jobname.bib}
@book{Hiemenz,
author={Hiemenz},
title={Polymer Chemistry}
}
\end{filecontents}

\usepackage{lipsum}

\usepackage{storebox}
\newstorebox{\mybox}

\usepackage{xskak}
\usepackage{chess-workshop-symbols}
\newcommand\mychessboard[1][]{\chessboard[#1]}

\usepackage[backend=biber,hyperref=auto,backref=true]{biblatex}
\addbibresource{\jobname.bib}

\usepackage{imakeidx}

\makeindex[intoc=true,columns=1,noautomatic]

\usepackage[hyperindex=true]{hyperref}

\title{Book}
\author{Author}
\date{}

\begin{document}

\maketitle

\frontmatter

\tableofcontents \newpage

\mainmatter

\chapter{C1}

aword\index{aword}

\newchessgame

This is a chessboard.

\mychessboard

\storebox{\mybox}{\mychessboard}

This is a chessboard from storebox.

\usestorebox{\mybox}

Here comes a quotation \cite{Hiemenz}.

This is a chessboard from storebox.

\usestorebox{\mybox}

\newpage

\lipsum[1]

bword\index{bword}

\backmatter

\printbibliography \newpage
\printindex

\end{document}

配置文件是:

\RequirePackage{addfont4ht} % needed for skaknew font
%
% save the clearpage before it is redefined by tex4ht
%
\let\oldclrearpage\clearpage
%
% define macro for newpage insertion
%
\def\mypagebreak{\Configure{newpage}{\ifvmode\IgnorePar\fi\EndP\HCode{<div class="newpage"></div>}}}
%
% new chessboard command
%
\renewcommand\mychessboard[1][]{\HCode{<div class="chessboard">}\Picture*{}\chessboard[#1]\EndPicture\HCode{</div>}}
%
\Preamble{xhtml}
%
% italic and bold
%
\Configure{textit}{\HCode{<span class="italic">}\NoFonts}{\EndNoFonts\HCode{</span>}}
\Configure{textbf}{\HCode{<span class="bold">}\NoFonts}{\EndNoFonts\HCode{</span>}}
\Css{.bold{font-weight:bold;}}
\Css{.italic{font-style:italic;}}
%
\catcode`\:=12
%
% enforce newpage
%
% define it for \newpage
%
\mypagebreak
\Css{.newpage{page-break-before:always;}}
%
% modify \Configure{BODY} so our configurations work on all extracted pages
%
\Configure{@BODY}{\def\clearpage{\bgroup\mypagebreak\oldclrearpage\egroup}}
\Configure{@/BODY}{\global\let\clearpage\oldclrearpage\Configure{newpage}{}}
%
% skak fonts
%
\AddFontFace{SkakNewFigurine}{SkakNew-Figurine}{SkakNew-Figurine.woff}{}
\Css{.SkakNew-Figurine-{font-family:"SkakNewFigurine",sans-serif;}}
%\Css{.SkakNew-Figurine-{font-family:"SkakNewFigurine",serif;}}
%
\begin{document}
%
\EndPreamble

构建文件是:

local filter    = require "make4ht-filter"

local process = filter{"cleanspan", "fixligatures"}

local used_images = {}
Make:image("png$", function(par)
  local output = par.output
  if not used_images[output] then
    local command = "dvipng -bg Transparent -T tight -D 167 -o ${output}  -pp ${page} ${source}" % par
    print(command)
    used_images[output] = true
    os.execute(command)
  end
  return true
end)

Make:add("biber","biber ${input}")

Make:add("xindy", function(par)
  -- par.encoding  = par.encoding or "utf8"
  -- par.language = par.language or "english"
  par.idxfile = par.idxfile or par.input .. ".idx"
  local modules = par.modules or {}
  local t = {}
  for k,v in ipairs(modules) do
    t[#t+1] = "-M ".. v
  end
  par.moduleopt = table.concat(t, " ")
  local xindy_call = "xindy -L ${language} -C ${encoding} ${moduleopt} ${idxfile}" % par
  print(xindy_call)
  return os.execute("xindy -L ${language} -C ${encoding} ${moduleopt} ${idxfile}" % par)
end, {modules = {"texindy"}, language = "english", encoding = "utf8"})

if mode=="draft" then
  Make:htlatex {}
else
  Make:htlatex {}
  Make:biber {}
  -- xindymodule = Make.params["input"]
  Make:xindy {modules = {"sample"}}
  Make:htlatex {}
  Make:htlatex {}
  Make:htlatex {}
end

Make:match("html$",process)

当我使用 epubcheck 检查 epub 时,日志显示错误:

Validating using EPUB version 2.0.1 rules.
ERROR(RSC-005): sample.epub/OEBPS/samplech1.html(19,63): Error while parsing file 'element "div" not allowed here; expected the element end-tag, text or element "a", "abbr", "acronym", "applet", "b", "bdo", "big", "br", "cite", "code", "del", "dfn", "em", "i", "iframe", "img", "ins", "kbd", "map", "noscript", "ns:svg", "object", "q", "samp", "script", "small", "span", "strong", "sub", "sup", "tt" or "var" (with xmlns:ns="http://www.w3.org/2000/svg")'.
ERROR(RSC-005): sample.epub/OEBPS/samplech1.html(36,32): Error while parsing file 'element "p" not allowed here; expected the element end-tag, text or element "a", "abbr", "acronym", "applet", "b", "bdo", "big", "br", "cite", "code", "del", "dfn", "em", "i", "iframe", "img", "ins", "kbd", "map", "noscript", "ns:svg", "object", "q", "samp", "script", "small", "span", "strong", "sub", "sup", "tt" or "var" (with xmlns:ns="http://www.w3.org/2000/svg")'.
ERROR(RSC-005): sample.epub/OEBPS/samplech1.html(37,32): Error while parsing file 'element "p" not allowed here; expected the element end-tag, text or element "a", "abbr", "acronym", "applet", "b", "bdo", "big", "br", "cite", "code", "del", "dfn", "em", "i", "iframe", "img", "ins", "kbd", "map", "noscript", "ns:svg", "object", "q", "samp", "script", "small", "span", "strong", "sub", "sup", "tt" or "var" (with xmlns:ns="http://www.w3.org/2000/svg")'.
ERROR(RSC-005): sample.epub/OEBPS/samplech1.html(41,36): Error while parsing file 'element "p" not allowed here; expected the element end-tag, text or element "a", "abbr", "acronym", "applet", "b", "bdo", "big", "br", "cite", "code", "del", "dfn", "em", "i", "iframe", "img", "ins", "kbd", "map", "noscript", "ns:svg", "object", "q", "samp", "script", "small", "span", "strong", "sub", "sup", "tt" or "var" (with xmlns:ns="http://www.w3.org/2000/svg")'.
ERROR(RSC-005): sample.epub/OEBPS/samplech1.html(41,63): Error while parsing file 'element "div" not allowed here; expected the element end-tag, text or element "a", "abbr", "acronym", "applet", "b", "bdo", "big", "br", "cite", "code", "del", "dfn", "em", "i", "iframe", "img", "ins", "kbd", "map", "noscript", "ns:svg", "object", "q", "samp", "script", "small", "span", "strong", "sub", "sup", "tt" or "var" (with xmlns:ns="http://www.w3.org/2000/svg")'.
ERROR(RSC-005): sample.epub/OEBPS/samplech1.html(57,36): Error while parsing file 'element "p" not allowed here; expected the element end-tag, text or element "a", "abbr", "acronym", "applet", "b", "bdo", "big", "br", "cite", "code", "del", "dfn", "em", "i", "iframe", "img", "ins", "kbd", "map", "noscript", "ns:svg", "object", "q", "samp", "script", "small", "span", "strong", "sub", "sup", "tt" or "var" (with xmlns:ns="http://www.w3.org/2000/svg")'.
ERROR(RSC-005): sample.epub/OEBPS/samplech1.html(59,36): Error while parsing file 'element "p" not allowed here; expected the element end-tag, text or element "a", "abbr", "acronym", "applet", "b", "bdo", "big", "br", "cite", "code", "del", "dfn", "em", "i", "iframe", "img", "ins", "kbd", "map", "noscript", "ns:svg", "object", "q", "samp", "script", "small", "span", "strong", "sub", "sup", "tt" or "var" (with xmlns:ns="http://www.w3.org/2000/svg")'.
ERROR(RSC-005): sample.epub/OEBPS/samplech1.html(63,36): Error while parsing file 'element "p" not allowed here; expected the element end-tag, text or element "a", "abbr", "acronym", "applet", "b", "bdo", "big", "br", "cite", "code", "del", "dfn", "em", "i", "iframe", "img", "ins", "kbd", "map", "noscript", "ns:svg", "object", "q", "samp", "script", "small", "span", "strong", "sub", "sup", "tt" or "var" (with xmlns:ns="http://www.w3.org/2000/svg")'.
ERROR(RSC-005): sample.epub/OEBPS/samplech1.html(63,63): Error while parsing file 'element "div" not allowed here; expected the element end-tag, text or element "a", "abbr", "acronym", "applet", "b", "bdo", "big", "br", "cite", "code", "del", "dfn", "em", "i", "iframe", "img", "ins", "kbd", "map", "noscript", "ns:svg", "object", "q", "samp", "script", "small", "span", "strong", "sub", "sup", "tt" or "var" (with xmlns:ns="http://www.w3.org/2000/svg")'.
ERROR(RSC-005): sample.epub/OEBPS/samplech1.html(80,25): Error while parsing file 'element "div" not allowed here; expected the element end-tag, text or element "a", "abbr", "acronym", "applet", "b", "bdo", "big", "br", "cite", "code", "del", "dfn", "em", "i", "iframe", "img", "ins", "kbd", "map", "noscript", "ns:svg", "object", "q", "samp", "script", "small", "span", "strong", "sub", "sup", "tt" or "var" (with xmlns:ns="http://www.w3.org/2000/svg")'.
ERROR(RSC-005): sample.epub/OEBPS/samplech1.html(84,32): Error while parsing file 'element "p" not allowed here; expected the element end-tag, text or element "a", "abbr", "acronym", "applet", "b", "bdo", "big", "br", "cite", "code", "del", "dfn", "em", "i", "iframe", "img", "ins", "kbd", "map", "noscript", "ns:svg", "object", "q", "samp", "script", "small", "span", "strong", "sub", "sup", "tt" or "var" (with xmlns:ns="http://www.w3.org/2000/svg")'.
ERROR(RSC-005): sample.epub/OEBPS/samplech1.html(100,36): Error while parsing file 'element "p" not allowed here; expected the element end-tag, text or element "a", "abbr", "acronym", "applet", "b", "bdo", "big", "br", "cite", "code", "del", "dfn", "em", "i", "iframe", "img", "ins", "kbd", "map", "noscript", "ns:svg", "object", "q", "samp", "script", "small", "span", "strong", "sub", "sup", "tt" or "var" (with xmlns:ns="http://www.w3.org/2000/svg")'.
ERROR(RSC-005): sample.epub/OEBPS/samplech1.html(103,25): Error while parsing file 'element "div" not allowed here; expected the element end-tag, text or element "a", "abbr", "acronym", "applet", "b", "bdo", "big", "br", "cite", "code", "del", "dfn", "em", "i", "iframe", "img", "ins", "kbd", "map", "noscript", "ns:svg", "object", "q", "samp", "script", "small", "span", "strong", "sub", "sup", "tt" or "var" (with xmlns:ns="http://www.w3.org/2000/svg")'.
ERROR(RSC-005): sample.epub/OEBPS/samplech1.html(107,25): Error while parsing file 'element "div" not allowed here; expected the element end-tag, text or element "a", "abbr", "acronym", "applet", "b", "bdo", "big", "br", "cite", "code", "del", "dfn", "em", "i", "iframe", "img", "ins", "kbd", "map", "noscript", "ns:svg", "object", "q", "samp", "script", "small", "span", "strong", "sub", "sup", "tt" or "var" (with xmlns:ns="http://www.w3.org/2000/svg")'.
ERROR(RSC-005): sample.epub/OEBPS/samplech1.html(111,40): Error while parsing file 'element "div" not allowed here; expected the element end-tag, text or element "a", "abbr", "acronym", "applet", "b", "bdo", "big", "br", "cite", "code", "del", "dfn", "em", "i", "iframe", "img", "ins", "kbd", "map", "noscript", "ns:svg", "object", "q", "samp", "script", "small", "span", "strong", "sub", "sup", "tt" or "var" (with xmlns:ns="http://www.w3.org/2000/svg")'.
FATAL(RSC-016): sample.epub/OEBPS/samplech1.html(113,3): Fatal Error while parsing file 'The element type "p" must be terminated by the matching end-tag "</p>".'.
ERROR(RSC-005): sample.epub/OEBPS/samplech1.html(-1,-1): Error while parsing file 'The element type "p" must be terminated by the matching end-tag "</p>".'.
FATAL(RSC-016): sample.epub/OEBPS/sample-1.svg(1,1): Fatal Error while parsing file 'Premature end of file.'.
ERROR(RSC-005): sample.epub/OEBPS/sample-1.svg(-1,-1): Error while parsing file 'Premature end of file.'.
FATAL(RSC-016): sample.epub/OEBPS/sample-2.svg(1,1): Fatal Error while parsing file 'Premature end of file.'.
ERROR(RSC-005): sample.epub/OEBPS/sample-2.svg(-1,-1): Error while parsing file 'Premature end of file.'.
FATAL(RSC-016): sample.epub/OEBPS/sample-3.svg(1,1): Fatal Error while parsing file 'Premature end of file.'.
ERROR(RSC-005): sample.epub/OEBPS/sample-3.svg(-1,-1): Error while parsing file 'Premature end of file.'.
FATAL(RSC-016): sample.epub/OEBPS/sample-4.svg(1,1): Fatal Error while parsing file 'Premature end of file.'.
ERROR(RSC-005): sample.epub/OEBPS/sample-4.svg(-1,-1): Error while parsing file 'Premature end of file.'.
FATAL(RSC-016): sample.epub/OEBPS/sample-5.svg(1,1): Fatal Error while parsing file 'Premature end of file.'.
ERROR(RSC-005): sample.epub/OEBPS/sample-5.svg(-1,-1): Error while parsing file 'Premature end of file.'.
FATAL(RSC-016): sample.epub/OEBPS/sample-6.svg(1,1): Fatal Error while parsing file 'Premature end of file.'.
ERROR(RSC-005): sample.epub/OEBPS/sample-6.svg(-1,-1): Error while parsing file 'Premature end of file.'.

使用 Kindlegen 也会出现错误:

*************************************************************
 Amazon kindlegen(Windows) V2.9 build 1029-0897292
 A command line e-book compiler
 Copyright Amazon.com and its Affiliates 2014
*************************************************************

Info(prcgen):I1047: Added metadata dc:Title        "Book"
Info(prcgen):I1047: Added metadata dc:Date         "2017-02-01T14:26:00Z"
Info(prcgen):I1047: Added metadata dc:Creator      "Author"
Info(prcgen):I1002: Parsing files  0000005
Warning(htmlprocessor):W28003: Value specified for CSS property in content is not supported by Kindle readers. Please refer Kindle Publishing Guidelines about usage of property: 'position: relative' in file: C:\Users\User\AppData\Local\Temp\mbp_7E1_2_1_E_2A_3A_EF_2114_B00_1\OEBPS\sample.css
Warning(inputpreprocessor):W29004: Forcefully closed opened Tag: <p class="indent">
      in file: C:\Users\User\AppData\Local\Temp\mbp_7E1_2_1_E_2A_3A_EF_2114_B00_1\OEBPS\samplech1.html     line: 0000035
Warning(inputpreprocessor):W29004: Forcefully closed opened Tag: <p class="indent">
      in file: C:\Users\User\AppData\Local\Temp\mbp_7E1_2_1_E_2A_3A_EF_2114_B00_1\OEBPS\samplech1.html     line: 0000041
Warning(inputpreprocessor):W29004: Forcefully closed opened Tag: <p class="indent">
      in file: C:\Users\User\AppData\Local\Temp\mbp_7E1_2_1_E_2A_3A_EF_2114_B00_1\OEBPS\samplech1.html     line: 0000056
Warning(inputpreprocessor):W29004: Forcefully closed opened Tag: <p class="indent">
      in file: C:\Users\User\AppData\Local\Temp\mbp_7E1_2_1_E_2A_3A_EF_2114_B00_1\OEBPS\samplech1.html     line: 0000078
Warning(prcgen):W14010: media file not found  C:\Users\User\AppData\Local\Temp\mbp_7E1_2_1_E_2A_3A_EF_2114_B00_1\OEBPS\sample-1.svg
Warning(prcgen):W14010: media file not found  C:\Users\User\AppData\Local\Temp\mbp_7E1_2_1_E_2A_3A_EF_2114_B00_1\OEBPS\sample-2.svg
Warning(prcgen):W14010: media file not found  C:\Users\User\AppData\Local\Temp\mbp_7E1_2_1_E_2A_3A_EF_2114_B00_1\OEBPS\sample-3.svg
Warning(prcgen):W14010: media file not found  C:\Users\User\AppData\Local\Temp\mbp_7E1_2_1_E_2A_3A_EF_2114_B00_1\OEBPS\sample-4.svg
Warning(prcgen):W14010: media file not found  C:\Users\User\AppData\Local\Temp\mbp_7E1_2_1_E_2A_3A_EF_2114_B00_1\OEBPS\sample-5.svg
Warning(prcgen):W14010: media file not found  C:\Users\User\AppData\Local\Temp\mbp_7E1_2_1_E_2A_3A_EF_2114_B00_1\OEBPS\sample-6.svg
Info(prcgen):I1015: Building PRC file
Info(prcgen):I1006: Resolving hyperlinks
Info(prcgen):I1049: Building table of content     URL: C:\Users\User\AppData\Local\Temp\mbp_7E1_2_1_E_2A_3A_EF_2114_B00_1\OEBPS\sample.ncx
Warning(prcgen):W14016: Cover not specified
Info(pagemap):I8000: No Page map found in the book
Info(prcgen):I1045: Computing UNICODE ranges used in the book
Info(prcgen):I1046: Found UNICODE range: Basic Latin [20..7E]
Info(prcgen):I1017: Building PRC file, record count:   0000002
Info(prcgen):I1039: Final stats - text compressed to (in % of original size):  41.83%
Info(prcgen):I1040: The document identifier is: "Book"
Info(prcgen):I1041: The file format version is V6
Info(prcgen):I1031: Saving PRC file
Info(prcgen):I1033: PRC built with WARNINGS!
Info(prcgen):I1016: Building enhanced PRC file
Info(prcgen):I1007: Resolving mediaidlinks
Info(prcgen):I1011: Writing mediaidlinks
Info(prcgen):I1009: Resolving guide items
Info(prcgen):I1017: Building PRC file, record count:   0000006
Info(prcgen):I1039: Final stats - text compressed to (in % of original size):  36.92%
Info(prcgen):I1041: The file format version is V8
Info(prcgen):I1032: PRC built successfully
Info(prcgen):I15000:  Approximate Standard Mobi Deliverable file size :   0000125KB
Info(prcgen):I15001:  Approximate KF8 Deliverable file size :   0000139KB
Info(prcgen):I1037: Mobi file built with WARNINGS!

Calibre 可以毫无问题地显示这两个版本。

有没有办法修复这些错误或者我可以忽略它们?

谢谢。

答案1

问题主要有两个来源 - 首先是该chesboard包使用 Tikz 在棋盘上绘制一些东西并生成损坏的 SVG 图像,第二个问题是该包\usestorebox 和可能的其他一些配置在 HTML 中生成了错误的段落。

禁用 SVG 图像很容易,我们只需要使用不同的 Tikz 后端来生成绘图。在\usepackage{xskak}加载chessboard包之前添加以下代码:

\ifdefined\HCode
\def\pgfsysdriver{pgfsys-dvips.def}
\fi

要处理错误的段落标签,请尝试修改此.cfg文件:

\RequirePackage{addfont4ht} % needed for skaknew font
%
% save the clearpage before it is redefined by tex4ht
%
\let\oldclrearpage\clearpage
%
% define macro for newpage insertion
%
\def\mypagebreak{\Configure{newpage}{\ifvmode\IgnorePar\fi\EndP\HCode{<div class="newpage"></div>}}}
%
% new chessboard command
%
\renewcommand\mychessboard[1][]{\ifvmode\IgnorePar\fi\EndP\HtmlParOff\HCode{<div class="chessboard">}\Picture*{}\chessboard[#1]\EndPicture\HCode{</div>}\HtmlParOn}
\let\tmpusavebox\usestorebox
\renewcommand\usestorebox[1]{\ifvmode\IgnorePar\fi\EndP\HtmlParOff\tmpusavebox{#1}\HtmlParOn\ifvmode\IgnorePar\fi\par\EndP\HCode{ }}

%
\Preamble{xhtml}
%
% italic and bold
%
\Configure{textit}{\HCode{<span class="italic">}\NoFonts}{\EndNoFonts\HCode{</span>}}
\Configure{textbf}{\HCode{<span class="bold">}\NoFonts}{\EndNoFonts\HCode{</span>}}
\Css{.bold{font-weight:bold;}}
\Css{.italic{font-style:italic;}}
%
\catcode`\:=12
%
% enforce newpage
%
% define it for \newpage
%
\mypagebreak
\Css{.newpage{page-break-before:always;}}
%
% modify \Configure{BODY} so our configurations work on all extracted pages
%
\Configure{@BODY}{\def\clearpage{\bgroup\mypagebreak\oldclrearpage\egroup}\HtmlParOn}
\Configure{@/BODY}{\global\let\clearpage\oldclrearpage\Configure{newpage}{}\HtmlParOff}
%
% skak fonts
%
\AddFontFace{SkakNewFigurine}{SkakNew-Figurine}{SkakNew-Figurine.woff}{}
\Css{.SkakNew-Figurine-{font-family:"SkakNewFigurine",sans-serif;}}
%\Css{.SkakNew-Figurine-{font-family:"SkakNewFigurine",serif;}}
%
\begin{document}
%
\EndPreamble

主要区别在于:

\renewcommand\mychessboard[1][]{\ifvmode\IgnorePar\fi\EndP\HtmlParOff\HCode{<div class="chessboard">}\Picture*{}\chessboard[#1]\EndPicture\HCode{</div>}\HtmlParOn}

我添加了\HtmlParOff\HtmlParOn命令以禁止在棋盘中包含任何段落。否则,您将获得以下无效的 HTML 代码:

<!--l. 56--><p class="indent" >
<!--l. 58--><p class="indent" >   This is a chessboard from storebox.
</p>

   </p>

我们还需要</p>在以下情况下禁用错误的标签\usestorebox

\let\tmpusavebox\usestorebox
\renewcommand\usestorebox[1]{\ifvmode\IgnorePar\fi\EndP\HtmlParOff\tmpusavebox{#1}\HtmlParOn\ifvmode\IgnorePar\fi\par\EndP\HCode{ }}

最后的通配符:\HtmlParOn\ifvmode\IgnorePar\fi\par\EndP\HCode{ }请求在存储框后包含新段落。我意识到这有点神秘 :)

\Configure{@BODY}{\def\clearpage{\bgroup\mypagebreak\oldclrearpage\egroup}\HtmlParOn}
\Configure{@/BODY}{\global\let\clearpage\oldclrearpage\Configure{newpage}{}\HtmlParOff}

这将禁用标签后的段落包含,再次</html>使用。\HtmlParOff

epubcheck现在应该很高兴:

$ epubcheck sample.epub 
Validating against EPUB version 2.0 - custom validation
Validating using EPUB version 2.0 rules.
No errors or warnings detected.
epubcheck completed

相关内容