html转化为pdf-Pandoc:超强大的文档格式转换工具

文章目录

大家好,我是托尼老师,只讲技术,不剪头发。 最近发现了一款​​免费的文档格式转换工具:Pandoc,堪称该领域的利器,下面就给大家介绍一下。

潘多克简介

Pandoc是John MacFarlane开发的通用文档转换工具,可以支持大量标记语言之间的格式转换,例如Markdown、Microsoft Word、PowerPoint、Jupyter Notebook、HTML、PDF、LaTeX、Wiki和EPUB格式。 。 官方称其为该领域的“瑞士军刀”,并给出了格式转换示意图。

可以看出Pandoc支持的格式非常多; 关键是它是一个开源且免费的工具。 源代码放在GitHub上,使用Haskell编程语言实现。 具体来说,Pandoc支持以下格式之间的转换(←表示可以从该格式转换为另一种格式;→表示可以转换为该格式;支持该格式的单向转换):

HTML 格式 电子书 文档格式 Roff 格式 TeX 格式 XML 格式 大纲格式 数据格式 文字处理格式 交互式笔记格式 页面布局格式 Wiki 标记语言格式 幻灯片放映格式 自定义格式 PDF 下载和安装

Pandoc 提供了 Haskell 代码库和命令行程序,支持 Windows、macOS、Linux、Chrome OS、BSD、Docker、GitHub Actions 和源代码编译。 最简单的安装方式就是点击下载编译好的安装文件。

视窗

Pandoc提供了Windows系统编译好的msi安装包html转化为pdf,可以直接运行安装; 或者您可以直接下载zip文件而无需安装并解压。 另一种安装方式是使用 Chocolatey 来安装:

choco install pandoc

苹果系统

Pandoc提供了macOS系统编译好的pkg安装包,可以直接运行安装; 或者直接下载zip文件而不安装并解压。 另一种安装方式是使用 Homebrew 来安装:

brew install pandoc

Linux

对于Debian、Ubuntu、Slackware、Arch、Fedora、NiXOS、openSUSE、gentoo等主流Linux发行版,可以直接使用系统包管理器安装Pandoc。 同时Pandoc提供了amd64架构的二进制安装包。

其他操作系统及安装方法请参考官方文档。

初次使用

下面介绍一下Pandoc命令行工具的简单使用。 首先进入安装目录并运行pandoc或pandoc.exe:

d:Softwarepandoc-2.10.1>pandoc.exe --version
pandoc.exe 2.10.1
Compiled with pandoc-types 1.21, texmath 0.12.0.2, skylighting 0.8.5
Default user data directory: C:UsersdongxAppDataRoamingpandoc
Copyright (C) 2006-2020 John MacFarlane
Web:  https://pandoc.org
This is free software; see the source for copying conditions.
There is no warranty, not even for merchantability or fitness
for a particular purpose.

上述命令返回Pandoc的版本信息。

接下来我们测试一下文件格式转换,在当前目录下创建一个文本文件test.md,输入以下内容:

---
title: Test
...
# Test!
This is a test of *pandoc*.
- list one
- list two

然后在命令行输入以下命令:

pandoc.exe test.md -f markdown -t html -s -o test.html

文件名test.md为待转换的源文件; -f 设置输入文件的格式; -t 设置输出文件的格式; -s 表示创建一个“独立”文件,它将生成该文件

标题和页码。 默认的转换格式是markdown转HTML,所以前面的命令也可以省略这两个选项。

执行后,在当前目录下生成一个test.html,内容如下:


<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
  <meta charset="utf-8" />
  <meta name="generator" content="pandoc" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
  <title>Test</title>
  <style>
    code{white-space: pre-wrap;}
    span.smallcaps{font-variant: small-caps;}
    span.underline{text-decoration: underline;}
    div.column{display: inline-block; vertical-align: top; width: 50%;}
    div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
    ul.task-list{list-style: none;}
    .display.math{display: block; text-align: center; margin: 0.5rem auto;}
  </style>
  
</head>
<body>
<header id="title-block-header">
<h1 class="title">Test</h1>
</header>
<h1 id="test">Test!</h1>
<p>This is a test of <em>pandoc</em>.</p>
<ul>
<li>list one</li>
<li>list two</li>
</ul>
</body>
</html>

如果要将文件转换为LaTeX格式html转化为pdf,可以输入以下命令:

pandoc.exe test.md -f markdown -t latex -s -o test.tex

Pandoc可以根据文件名扩展猜测输入输出文件的格式,例如以下命令可以将文件转换为Word文档格式:

pandoc.exe test.md -s -o test.docx

如果您已经安装了 LaTeX,则可以使用以下命令转换为 PDF 文件:

pandoc.exe test.md -f markdown -s -o test.pdf

输入 pandoc --help 命令查看该工具的选项帮助,详细使用方法请参阅用户指南。

此外,Pandoc还提供了在线格式转换工具和各种格式转换示例。

Pandoc集成

除了使用命令行方式之外,很多开发工具和软件都集成了Pandoc来实现文件格式转换。 例如,Markdown 编辑器 PanWriter、Typora,文本编辑器 Atom、Sublime Text、Emacs、Vim、R Markdown、PanConvert、Manubot 等。

更多与 Pandoc 集成的第三方软件可以在这里找到。

相关资源汇总

Pandoc是一个免费开源的格式转换工具,可用于各种标记格式文档之间的转换。 Pandoc广泛应用于写作工作和电子书出版过程中。