{"id":19966663,"url":"https://github.com/truthhun/converter","last_synced_at":"2025-04-04T12:08:59.684Z","repository":{"id":44383753,"uuid":"117174360","full_name":"TruthHun/converter","owner":"TruthHun","description":"通过calibre将html转成epub、mobi、PDF等","archived":false,"fork":false,"pushed_at":"2022-12-20T03:04:40.000Z","size":19094,"stargazers_count":559,"open_issues_count":6,"forks_count":124,"subscribers_count":16,"default_branch":"master","last_synced_at":"2025-03-28T11:09:34.334Z","etag":null,"topics":["bookstack","calibre","ebook","ebook-convert","go","golang"],"latest_commit_sha":null,"homepage":"http://www.bookstack.cn","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TruthHun.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-01-12T01:16:29.000Z","updated_at":"2025-03-25T11:55:17.000Z","dependencies_parsed_at":"2023-01-29T23:31:11.127Z","dependency_job_id":null,"html_url":"https://github.com/TruthHun/converter","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TruthHun%2Fconverter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TruthHun%2Fconverter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TruthHun%2Fconverter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TruthHun%2Fconverter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TruthHun","download_url":"https://codeload.github.com/TruthHun/converter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247174423,"owners_count":20896078,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bookstack","calibre","ebook","ebook-convert","go","golang"],"created_at":"2024-11-13T02:37:31.450Z","updated_at":"2025-04-04T12:08:59.657Z","avatar_url":"https://github.com/TruthHun.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"目录\n- [缘由](#intro)\n- [安装calibre](#intro)\n- [文档导出效果](#effect)\n- [快速体验](#quick)\n- [使用案例](#example)\n- [配置文件](#config)\n\t- [json配置示例](#config-example)\n\t- [json配置项说明](#config-statement)\n- [使用教程](#tutorial)\n\t- [Go语言使用](#tutorial-go)\n\t- [其它语言](#tutorial-other)\n- [原理](#principle)\n- [注意事项](#attention)\n- [精神上支持我](#support)\n- [markdown文档如何转成pdf、epub、mobi](#markdown)\n\n通过calibre，将markdown和html转成epub、mobi、pdf等格式文档。\n\n\u003e 说明(于2018-06-02)：calibre将HTML转成mobi等格式时请注意配置好各项配置参数到合理值，同时注意自己的HTML的样式调整，特别是在mobi格式的时候，html的CSS样式，如`background:#dddddd`设置背景色，颜色不生效，需要用`background-color:#dddddd`才会生效。\n目前经过多次测试，生成文档效果不佳的问题，主要是配置参数如字体大小和CSS样式问题造成的。同时，为了方便在epub等阅读器上的阅读体验，可以将body标签的CSS背景色设置为透明。\n\n\n\n\u003ca name=\"intro\"\u003e\u003c/a\u003e\n## 缘由\n\n事情是这样的：我们公司 [掘金量化](https://www.myquant.cn)(跟[掘金](https://juejin.im)是`javascript`和`java`的关系) 让我开发公司官网，官网的帮助中心文档，需要生成pdf离线文档以方便用户阅读和使用，但是使用`wkhtmltopdf`生成PDF文档，效果不是很理想。\n\n看着gitbook和看云生成的PDF文档，非常完美，然后把从两个站点下载的PDF文档的后缀改成了`.xml`格式，文本编辑器打开一看，用的居然是`calibre`，然后当时捣鼓了半个月，终于把问题解决。\n\n感觉很多企业和个人可能也会有同样的需求，所以干脆将`calibre`的使用封装一下，开源出来，以方便更多的人。\n\n\u003e 当前文档，会告诉你使用方式和生成原理，以便你也可以根据自己掌握的编程语言封装一个工具类\n\n\u003ca name=\"install\"\u003e\u003c/a\u003e\n## 安装calibre\n- 下载地址：https://calibre-ebook.com/download\n- 根据自己的系统安装对应的calibre（需要注意的是，calibre要安装3.x版本的，2.x版本的功能不是很强大。反正安装最新的就好。）\n- 安装完calibre之后，将calibre加入到系统环境变量中，执行下面的命令之后显示3.x的版本即表示安装成功。\n```\nebook-convert --version\n```\n\u003ca name=\"effect\"\u003e\u003c/a\u003e\n## 文档导出效果\n案例使用的是无闻的《Gogs中文文档》，文档地址：https://www.bookstack.cn/books/gogs_zh ，可以访问该地址查看文档导出的实际效果。\n\n截图：\n![文档导出效果](effect.png)\n\n\u003ca name=\"quick\"\u003e\u003c/a\u003e\n## 快速体验\n如果成功安装了calibre，并配置了环境变量，则可以按照下面的方式进行快速体验。(bin目录下是各系统64位的二进制可执行程序，限64位系统)\n- Windows用户，进入`bin/win`目录，执行命令：\n```\n./converter.exe ../../example/gogs_zh/config.json\n```\n- Mac用户，进入`bin/mac`目录，执行命令：\n```\n./converter ../../example/gogs_zh/config.json\n```\n- Linux用户，进入`bin/linux`目录，执行命令：\n```\n./converter ../../example/gogs_zh/config.json\n```\n执行成功之后，在`example/gogs_zh/output`下可以看到导出的文档\n\n\u003ca name=\"example\"\u003e\u003c/a\u003e\n## 使用案例\n- 掘金量化 https://www.myquant.cn (这是我们公司的官网，做量化投资的。戳进去，\"帮助中心\"里面可以看到文档导出效果)\n- 书栈网(BookStack) https://www.bookstack.cn (基于MinDoc做的二次开发的站点，开源地址：https://github.com/TruthHun/BookStack)\n- MinDoc  https://github.com/lifei6671/mindoc\n\n\u003ca name=\"config\"\u003e\u003c/a\u003e\n## 配置文件\n以json作为配置文件，配置文件的文件名不限制，在使用的时候指定一下配置文件的文件名即可。\n需要注意的是，json配置文件需要放在项目的根目录下。比如示例项目，`example/gogz_zh`中，配置文件`config.json`就是放在项目的根目录\n\n\u003ca name=\"config-example\"\u003e\u003c/a\u003e\n### json配置示例\n```json\n{\n\t\"charset\": \"utf-8\",\n\t\"cover\": \"\",\n\t\"date\": \"2018-01-21\",\n\t\"description\": \"Gogs 的目标是打造一个最简单、最快速和最轻松的方式搭建自助 Git 服务。使用 Go 语言开发使得 Gogs 能够通过独立的二进制分发，并且支持 Go 语言支持的 所有平台，包括 Linux、Mac OS X、Windows 以及 ARM 平台。\",\n\t\"footer\": \"\u003cp style='color:#8E8E8E;font-size:12px;'\u003e本文档使用 \u003ca href='http://www.bookstack.cn' style='text-decoration:none;color:#1abc9c;font-weight:bold;'\u003e书栈(BookStack.CN)\u003c/a\u003e 构建 \u003cspan style='float:right'\u003e- _PAGENUM_ -\u003c/span\u003e\u003c/p\u003e\",\n\t\"header\": \"\u003cp style='color:#8E8E8E;font-size:12px;'\u003e_SECTION_\u003c/p\u003e\",\n\t\"identifier\": \"\",\n\t\"language\": \"zh-CN\",\n\t\"creator\": \"书栈(BookStack.CN)\",\n\t\"publisher\": \"书栈(BookStack.CN)\",\n\t\"contributor\": \"书栈(BookStack.CN)\",\n\t\"title\": \"Gogs中文文档\",\n\t\"format\": [\"epub\", \"mobi\", \"pdf\"],\n\t\"font_size\": \"14\",\n\t\"paper_size\": \"a4\",\n\t\"margin_left\": \"72\",\n\t\"margin_right\": \"72\",\n\t\"margin_top\": \"72\",\n\t\"margin_bottom\": \"72\",\n\t\"more\": [],\n\t\"toc\": [{\n\t\t\"id\": 709800000,\n\t\t\"link\": \"statement.html\",\n\t\t\"pid\": 0,\n\t\t\"title\": \"致谢\"\n\t}, {\n\t\t\"id\": 539,\n\t\t\"link\": \"539.html\",\n\t\t\"pid\": 538,\n\t\t\"title\": \"5.1 配置文件手册\"\n\t}, {\n\t\t\"id\": 545,\n\t\t\"link\": \"545.html\",\n\t\t\"pid\": 543,\n\t\t\"title\": \"4.1 自定义模板\"\n\t}, {\n\t\t\"id\": 549,\n\t\t\"link\": \"549.html\",\n\t\t\"pid\": 548,\n\t\t\"title\": \"2.1 注册 Windows 服务\"\n\t}, {\n\t\t\"id\": 554,\n\t\t\"link\": \"554.html\",\n\t\t\"pid\": 0,\n\t\t\"title\": \"1. 简介\"\n\t}, {\n\t\t\"id\": 555,\n\t\t\"link\": \"555.html\",\n\t\t\"pid\": 554,\n\t\t\"title\": \"1.1 变更日志\"\n\t}, {\n\t\t\"id\": 540,\n\t\t\"link\": \"540.html\",\n\t\t\"pid\": 538,\n\t\t\"title\": \"5.2 公告与高阶指南\"\n\t}, {\n\t\t\"id\": 547,\n\t\t\"link\": \"547.html\",\n\t\t\"pid\": 543,\n\t\t\"title\": \"4.2 Web 钩子\"\n\t}, {\n\t\t\"id\": 548,\n\t\t\"link\": \"548.html\",\n\t\t\"pid\": 0,\n\t\t\"title\": \"2. 下载安装\"\n\t}, {\n\t\t\"id\": 556,\n\t\t\"link\": \"556.html\",\n\t\t\"pid\": 554,\n\t\t\"title\": \"1.2 常见问题\"\n\t}, {\n\t\t\"id\": 559,\n\t\t\"link\": \"559.html\",\n\t\t\"pid\": 558,\n\t\t\"title\": \"3.1 二进制升级\"\n\t}, {\n\t\t\"id\": 546,\n\t\t\"link\": \"546.html\",\n\t\t\"pid\": 543,\n\t\t\"title\": \"4.3 多国语言支持\"\n\t}, {\n\t\t\"id\": 550,\n\t\t\"link\": \"550.html\",\n\t\t\"pid\": 548,\n\t\t\"title\": \"2.2 配置与运行\"\n\t}, {\n\t\t\"id\": 557,\n\t\t\"link\": \"557.html\",\n\t\t\"pid\": 554,\n\t\t\"title\": \"1.3 故障排查\"\n\t}, {\n\t\t\"id\": 558,\n\t\t\"link\": \"558.html\",\n\t\t\"pid\": 0,\n\t\t\"title\": \"3. 版本升级\"\n\t}, {\n\t\t\"id\": 560,\n\t\t\"link\": \"560.html\",\n\t\t\"pid\": 558,\n\t\t\"title\": \"3.2 源码升级\"\n\t}, {\n\t\t\"id\": 543,\n\t\t\"link\": \"543.html\",\n\t\t\"pid\": 0,\n\t\t\"title\": \"4. 功能介绍\"\n\t}, {\n\t\t\"id\": 544,\n\t\t\"link\": \"544.html\",\n\t\t\"pid\": 543,\n\t\t\"title\": \"4.4 授权认证\"\n\t}, {\n\t\t\"id\": 551,\n\t\t\"link\": \"551.html\",\n\t\t\"pid\": 548,\n\t\t\"title\": \"2.3 二进制安装\"\n\t}, {\n\t\t\"id\": 538,\n\t\t\"link\": \"538.html\",\n\t\t\"pid\": 0,\n\t\t\"title\": \"5. 高级用法\"\n\t}, {\n\t\t\"id\": 552,\n\t\t\"link\": \"552.html\",\n\t\t\"pid\": 548,\n\t\t\"title\": \"2.4 包管理安装\"\n\t}, {\n\t\t\"id\": 542,\n\t\t\"link\": \"542.html\",\n\t\t\"pid\": 0,\n\t\t\"title\": \"6. 捐赠我们\"\n\t}, {\n\t\t\"id\": 553,\n\t\t\"link\": \"553.html\",\n\t\t\"pid\": 548,\n\t\t\"title\": \"2.5 源码安装\"\n\t}]\n}\n```\n\n\u003ca name=\"config-statement\"\u003e\u003c/a\u003e\n### json配置项说明\n- `charset` 指定字符集，留空则默认为utf-8编码。选填。\n- `cover` 封面图片，一张`800x1068`像素或该尺寸比例的图片，jpg、png或gif格式。如果没有封面，请置空。选填。\n- `date` 文档生成日期，年月日时分秒都可以。默认当前日期时间。选填。\n- `title`  文档标题。必填。\n- `description` 文档描述、摘要。选填。\n- `footer`、`header` 生成PDF文档时，文档的底部(footer)和顶部(header)内容，仅对PDF文档有效。选填。\n- `identifier` 出版物的标识。留空即可。\n- `language`    语言。必填。可选值：zh、en、zh-CN、en-US等。\n- `creator`、`publisher`、`contributor`   如果你懒的话，三个都传同一个值，比如示例中的\"书栈(BookStack.CN)\"。如果真要区分的话，`creator`就是文档作者，比如\"进击的皇虫\"，`publisher`、`contributor`理解为出版单位和构建人。三个配置项，建议填写。\n- `format`  导出的文档格式，不传值则默认导出PDF。可选值：epub、pdf、mobi。\n- `font_size` 数字，默认字体大小，仅对导出PDF有效。选填。\n- `paper_size` 导出文档的页面大小，不区分大小写。默认\"A4\"，选填。可选值： `a0`, `a1`, `a2`, `a3`, `a4`, `a5`, `a6`, `b0`, `b1`, `b2`, `b3`, `b4`,`b5`, `b6`, `legal`, `letter`\n- `margin_left`、`margin_right`、`margin_top`、`margin_bottom`  数字，左边距、右边距、上边距、下边距，仅对PDF文档有效。选填，默认72，即表示72pt。\n- `more`    更多选项。仅对PDF有效。不建议使用。\n- `toc` 重要！！！这个是重中之重，用于生成文档目录的。`id`、`pid`是数字，`pid`表示上级的id。id的值不能重复。`link`表示html链接文件。toc里面的`title`表示目录章节标题。\n\n(参照示例的`config.json`去配置就好。)\n\n\u003ca name=\"tutorial\"\u003e\u003c/a\u003e\n## 使用教程\nbin目录下的是当前程序生成的64位的二进制可执行文件。Windows下使用示例：\n```\nconverter.exe path/to/config.json\n```\n执行成功之后，会自动在项目目录下创建个`output`文件夹，并将文件导出到里面。比如导出pdf格式，则会在`output`文件夹下面出现`book.pdf`文件。\n\n\u003ca name=\"tutorial-go\"\u003e\u003c/a\u003e\n### Go语言使用\n引入当前包：\n`github.com/TruthHun/converter/converter`\n```golang\nif Convert, err:= converter.NewConverter(\"path/to/config.json\");err==nil{\n    Convert.Convert()\n}\n```\n\n\u003ca name=\"tutorial-other\"\u003e\u003c/a\u003e\n### 其它语言\n由于目前没封装PHP、Python等的类和包，所以其它语言要使用的话，就是在项目下生成一个config.json(名字随便自己定义)，然后调用各自语言的cmd执行：\n```\n/path/to/converter /path/to/config.json\n```\n\u003ca name=\"principle\"\u003e\u003c/a\u003e\n## 原理\nHTML导出PDF、epub等文档的原理很简单：根据config.json中的内容，生成epub电子书的基本结构，然后将当前目录下的文件压缩并重命名如`content.epub`，然后再使用`ebook-convert`进行转换，转换命令：\n```\nebook-convert content.epub output/book.pdf [options]\n```\n这样，你就可以使用自己熟悉的语言封装一个包，并调用calibre导出文档了。\n\n\n\u003ca name=\"attention\"\u003e\u003c/a\u003e\n## 注意事项\n- HTML中不要有使用js代码渲染文档内容，因为js是不会被执行的\n\n\u003ca name=\"support\"\u003e\u003c/a\u003e\n## 精神上支持我\n虽然我很缺钱...\n但是得到他人的肯定和认可比什么都重要。\n如果当前项目帮到了你，请给项目一个star，以鼓励我在开源的路上能走的更好、更远。\n附自己业余时间搞的一些站点，支持我，可以点击访问一下：\n- IT文库  http://wenku.it\n- 书栈(BookStack) http://www.bookstack.cn\n\n\u003ca name=\"markdown\"\u003e\u003c/a\u003e\n## markdown文档如何转成pdf、epub、mobi\n说实话，这个我没仔细去研究过。思路就是将markdown文档转成HTML(毕竟这样比较好控制显示样式)，然后再通过当前工具再转成PDF文档等。网上应该有更好的方法，如果大家找到了，麻烦也分享一下。\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftruthhun%2Fconverter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftruthhun%2Fconverter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftruthhun%2Fconverter/lists"}