简体   繁体   English

使用 Pandoc 将 Markdown 转换为 PDF 时使用 CSS

[英]Using CSS when converting Markdown to PDF with Pandoc

I'm trying out Pandoc on OS X, and results thus far are impressive.我正在 OS X 上试用 Pandoc,迄今为止的结果令人印象深刻。 One blocking problem, however, is getting CSS styles to work on inline code samples.然而,一个阻塞问题是让 CSS 样式适用于内联代码示例。 I'm converting from Markdown to PDF.我正在从 Markdown 转换为 PDF。

I have this string in my source:我的源中有这个字符串:

* Create a simple HTML document (<span class="filename">simple.html</span>) and load it into the browser via the file system

I've also tried this:我也试过这个:

* Create a simple HTML document (`simple.html`{.filename}) and load it into the browser via the file system

I'd like to apply the class "filename" to the enclosed text in each case, but it doesn't seem to do anything to the output.我想在每种情况下将类“文件名”应用于封闭的文本,但它似乎对输出没有任何作用。 However the manual says:但是手册上说:

Some output formats can use this information to do syntax highlighting.某些输出格式可以使用此信息进行语法高亮显示。 Currently, the only output formats that uses this information are HTML and LaTeX.目前,唯一使用此信息的输出格式是 HTML 和 LaTeX。

Here's my command:这是我的命令:

pandoc \
    --output ./output.pdf \
    --css source/styles.css \
    source/en/docs/input.md

I'm converting to PDF, which is written by LaTeX by Pandoc internally.我正在转换为 PDF,它是由 Pandoc 在内部由 LaTeX 编写的。 Can I get this to work?我可以让这个工作吗? Or, can I use a style defined using a LaTeX command?或者,我可以使用使用 LaTeX 命令定义的样式吗? - it doesn't have to be CSS. - 它不必是 CSS。 However, it must be a style system - it's not workable to change italic/font/colour attributes on each occasion.但是,它必须是一个样式系统 - 在每种情况下都无法更改斜体/字体/颜色属性。

I've tried sending output temporarily to HTML, and in that situation the styles are imported directly from the specific style asset.我尝试将输出临时发送到 HTML,在这种情况下,样式是直接从特定样式资产导入的。 So, my stylesheet specification and span markup is correct, at least for one output format.所以,我的样式表规范和跨度标记是正确的,至少对于一种输出格式是正确的。

Addenda附录

A couple of afterthoughts:一些事后的想法:

  • The solution does not have to be Pandoc or Markdown.解决方案不一定是 Pandoc 或 Markdown。 However, it does need to be a trivial text-based markup language that can convert reliably to PDF, as I want to store the document files on Git for easy forking and merging.但是,它确实需要是一种简单的基于文本的标记语言,可以可靠地转换为 PDF,因为我想将文档文件存储在 Git 上以便于分叉和合并。 I'm not keen on HTML as it is verbose, and engines to convert it aren't that great (though, admittedly, my formatting requirements are modest).我不喜欢 HTML,因为它很冗长,而且转换它的引擎也不是很好(不过,无可否认,我的格式要求并不高)。
  • The HTML output from Pandoc is fine, so if I can find something that converts the (simple) HTML/CSS to PDF reliably, I'll be fine. Pandoc 的 HTML 输出很好,所以如果我能找到将(简单的)HTML/CSS 可靠地转换为 PDF 的东西,我会没事的。 Of course, Pandoc should be able to do this, but inline styles (for the background colour on code fragments) aren't rendered.当然,Pandoc 应该能够做到这一点,但内联样式(用于代码片段的背景颜色)不会呈现。 This might be a faff, as I'll have to reintroduce things like page-breaks, which can be non-trivial in HTML-to-PDF converters.这可能是废话,因为我将不得不重新引入诸如分页符之类的东西,这在 HTML 到 PDF 转换器中可能非常重要。

"I'd like to apply the class "filename" to the enclosed text in each case, but it doesn't seem to do anything to the output." “我想在每种情况下将“文件名”类应用于所附文本,但它似乎对输出没有任何作用。”

It works for HTML.它适用于 HTML。 Running Pandoc interactively, ^D to see the resulting code:以交互方式运行 Pandoc, ^D以查看结果代码:

$>  pandoc -f markdown -t html

* Create a simple HTML document (`simple.html`{.filename}) and load it.

^D

<ul>
<li>Create a simple HTML document (<code class="filename">simple.html</code>) and load it.</li>
</ul>

It doesn't work for LaTeX if you use the .filename class.如果您使用.filename类,它不适用于 LaTeX。 You need to use one of the known classnames:您需要使用已知的类名之一:

$>  pandoc -f markdown -t latex

* Create a simple HTML document (`simple.html`{.filename}) and load it.

^D

\begin{itemize}
\tightlist
\item
  Create a simple HTML document (\texttt{simple.html}) and load it.
\end{itemize}

Now using one of the known classnames, like .bash , .postscript , .php , ...:现在使用已知的类名之一,如.bash.postscript.php 、 ...:

$>  pandoc -f markdown -t latex

* Create a simple HTML document (`simple.html`{.bash}) and load it.

^D

\begin{itemize}
\tightlist
\item
  Create a simple HTML document (\VERB|\KeywordTok{simple.html}| and
  load it.
\end{itemize}

To convert HTML + CSS into PDF, you can also look into PrinceXML , which is free for non-commercial use.要将 HTML + CSS 转换为 PDF,您还可以查看PrinceXML ,它可免费用于非商业用途。

I don't know LaTeX at all, but have hacked this solution using this helpful manual .我根本不知道 LaTeX,但是已经使用这个有用的手册破解了这个解决方案。 First, create a style:首先,创建一个样式:

\definecolor{silver}{RGB}{230,230,230}

\newcommand{\inlinecodeblock}[1]{
    \colorbox{silver}{
        \texttt{#1}
    }
}

And here's how to use it:以下是如何使用它:

Some \inlinecodeblock{inline code}, and some widgets, go here

This creates a style with a background colour and a monospaced font.这将创建具有背景颜色和等宽字体的样式。 The margin and padding are a bit large for my preferences, but it's a very useable start.根据我的喜好,边距和填充有点大,但这是一个非常有用的开始。 Here's what it looks like:这是它的样子:

内联格式的屏幕截图

The disadvantage is that if I wish to output to a format that supports styles proper (such as HTML) then these are lost.缺点是,如果我希望输出为支持正确样式(例如 HTML)的格式,那么这些都会丢失。 Also, my solution only works with LaTeX/PDF.此外,我的解决方案仅适用于 LaTeX/PDF。 Thus, if you can fix these issues, please add a better answer!因此,如果您可以解决这些问题,请添加更好的答案!


Addendum: I have a better approach, which is thus:附录:我有一个更好的方法,因此:

\newcommand{\inlinecodeblock}[1]{
    \fboxsep 1pt
    \fboxrule 0pt
    \colorbox{silver}{\strut{\texttt{#1}}}
}

This avoids the problem of excess horizontal padding - I think it was the line break in the colorbox parameter that did it.这避免了水平填充过多的问题 - 我认为是colorbox参数中的colorbox造成的。 I've added in strut , which keeps highlights the same height regardless of whether the text has descenders.我添加了strut ,无论文本是否有strut ,它都会保持高亮显示相同的高度。

It's not perfect though - there's still too much horizontal margin outside the box, and a comma after a box will still orphan onto the next line.虽然它并不完美 - 框外的水平边距仍然太多,框后的逗号仍然会孤立到下一行。 I may give up with LaTeX, and render to HTML from Pandoc, and then use wkhtmltopdf to render the final document.我可能会放弃 LaTeX,从 Pandoc 渲染为 HTML,然后使用wkhtmltopdf渲染最终文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM