github开源文章生成pdf

最近需要研究ELK,然后在网上发现了有本书写的不错,然后搜到是在 github 上开源过的。这本书的时间有点久了,就想通过源码自己来生成一个 pdf

我使用的是 ubuntu 系统

step1:安装 gitbook 工具

gitbook 依赖与nodejs,我刚开始尝试源码安装,后来遇到 npm 的一个环境问题放弃了。如果需要手动安装的自行查找教程。

我使用的是:apt-get install -y node

sudo apt-get install nodejs
sudo apt-get install npm

使用 npm 安装 gitbook

npm install gitbook-cli -g

step2:下载源码

root@pc:/mnt/share# git clone https://github.com/chenryn/ELKstack-guide-cn.git
正克隆到 'ELKstack-guide-cn'...
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 2349 (delta 0), reused 0 (delta 0), pack-reused 2348
接收对象中: 100% (2349/2349), 8.61 MiB | 1.53 MiB/s, 完成.
处理 delta 中: 100% (1395/1395), 完成.
正在检出文件: 100% (287/287), 完成.

step3:进入到源码目录,执行命令 gitbook build

root@pc:/mnt/share/ELKstack-guide-cn# gitbook build
info: 7 plugins are installed info: 6 explicitly listed info: loading plugin "highlight"... OK info: loading plugin "search"... OK info: loading plugin "lunr"... OK info: loading plugin "sharing"... OK info: loading plugin "fontsettings"... OK info: loading plugin "theme-default"... OK info: found 157 pages info: found 128 asset files info: >> generation finished with success in 43.8s !

step4:gitbook pdf 生成pdf

root@pc:/mnt/share/ELKstack-guide-cn# gitbook pdf
info: 7 plugins are installed 
info: 6 explicitly listed 
info: loading plugin "highlight"... OK 
info: loading plugin "search"... OK 
info: loading plugin "lunr"... OK 
info: loading plugin "sharing"... OK 
info: loading plugin "fontsettings"... OK 
info: loading plugin "theme-default"... OK 
info: found 157 pages 
info: found 128 asset files 
info: >> generation finished with success in 266.9s ! 
info: >> 1 file(s) generated 

 

注意:如果 gitbook 命令执行过程中,提示缺少插件,再执行 gitbook install 安装

最后会在当前目录生成 pdf 文件

gitbook 支持的格式有 ePub, Mobi, PDF

转载于:https://www.cnblogs.com/newguy/p/10033943.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MFC(Microsoft Foundation Class)是微软公司提供的一套C++类库,用于开发Windows应用程序。PDF(Portable Document Format)是一种跨平台的文档格式,可以在不同设备和操作系统上显示和打印。本文将介绍如何使用MFC解析PDF文件,并生成单色位图。 1. 安装PDFium库 PDFium是一个开源PDF解析库,可以用于解析PDF文件并提取文本、图像等内容。在使用MFC解析PDF文件之前,需要先安装PDFium库。可以从GitHub上下载PDFium源代码,并编译生成静态库或动态链接库。 2. 打开PDF文件 使用MFC打开PDF文件可以使用CFile类和CFileException类。首先使用CFile类打开PDF文件,代码如下: CFile pdfFile; if (!pdfFile.Open(_T("test.pdf"), CFile::modeRead | CFile::typeBinary)) { AfxMessageBox(_T("Failed to open PDF file!")); return; } 如果PDF文件打开失败,会弹出一个错误提示框。如果文件打开成功,则可以从文件中读取PDF数据。 3. 解析PDF文件 使用PDFium库解析PDF文件可以使用FPDF_LoadDocument函数和FPDF_CloseDocument函数。首先使用FPDF_LoadDocument函数加载PDF文件,代码如下: FPDF_DOCUMENT pdfDoc = FPDF_LoadDocument(pdfFile.GetFilePath(), nullptr); 如果PDF文件加载失败,可以使用FPDF_GetLastError函数获取错误信息。如果PDF文件加载成功,则可以使用FPDF_GetPageCount函数获取PDF页面数,并使用FPDF_LoadPage函数加载PDF页面。 4. 渲染PDF页面 使用PDFium库渲染PDF页面可以使用FPDF_RenderPageBitmap函数。首先创建一个位图对象,代码如下: CBitmap bmp; bmp.CreateBitmap(width, height, 1, 1, nullptr); 其中width和height是位图的宽度和高度,1表示每个像素占用1位(即单色位图),nullptr表示位图数据为空。然后使用FPDF_RenderPageBitmap函数将PDF页面渲染到位图中,代码如下: BITMAP bmpInfo; bmp.GetBitmap(&bmpInfo); int stride = ((bmpInfo.bmWidth + 31) / 32) * 4; BYTE* buffer = new BYTE[stride * bmpInfo.bmHeight]; FPDF_RenderPageBitmap(bitmap, pdfPage, 0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, 0, buffer, stride); 其中stride是位图每行数据的字节数,buffer是位图数据缓冲区。渲染完成后,可以将位图保存成BMP、PNG等格式的文件。 5. 关闭PDF文件 使用PDFium库关闭PDF文件可以使用FPDF_CloseDocument函数,代码如下: FPDF_CloseDocument(pdfDoc); 6. 完整代码 下面是使用MFC解析PDF生成单色位图的完整代码: #include "stdafx.h" #include "pdfium/include/fpdfview.h" #include "pdfium/include/fpdf_save.h" void GenerateMonochromeBitmap(const CString& pdfFilePath, const CString& bmpFilePath, int width, int height) { CFile pdfFile; if (!pdfFile.Open(pdfFilePath, CFile::modeRead | CFile::typeBinary)) { AfxMessageBox(_T("Failed to open PDF file!")); return; } FPDF_DOCUMENT pdfDoc = FPDF_LoadDocument(pdfFile.GetFilePath(), nullptr); if (!pdfDoc) { AfxMessageBox(_T("Failed to load PDF document!")); return; } int pageCount = FPDF_GetPageCount(pdfDoc); if (pageCount == 0) { AfxMessageBox(_T("No pages found in PDF document!")); FPDF_CloseDocument(pdfDoc); return; } FPDF_PAGE pdfPage = FPDF_LoadPage(pdfDoc, 0); if (!pdfPage) { AfxMessageBox(_T("Failed to load PDF page!")); FPDF_CloseDocument(pdfDoc); return; } CBitmap bmp; bmp.CreateBitmap(width, height, 1, 1, nullptr); BITMAP bmpInfo; bmp.GetBitmap(&bmpInfo); int stride = ((bmpInfo.bmWidth + 31) / 32) * 4; BYTE* buffer = new BYTE[stride * bmpInfo.bmHeight]; FPDF_RenderPageBitmap(bitmap, pdfPage, 0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, 0, buffer, stride); FPDF_ClosePage(pdfPage); FPDF_CloseDocument(pdfDoc); pdfFile.Close(); CImage image; image.Create(bmpInfo.bmWidth, bmpInfo.bmHeight, 1, 0); BYTE* dst = (BYTE*)image.GetBits(); BYTE* src = buffer; for (int y = 0; y < bmpInfo.bmHeight; y++) { for (int x = 0; x < bmpInfo.bmWidth; x++) { int pixelIndex = x / 8; int bitIndex = 7 - x % 8; BYTE pixel = (src[pixelIndex] >> bitIndex) & 0x01; dst[x] = pixel ? 0 : 255; } src += stride; dst += bmpInfo.bmWidth; } image.Save(bmpFilePath, Gdiplus::ImageFormatPNG); delete[] buffer; } int main() { CString pdfFilePath = _T("test.pdf"); CString bmpFilePath = _T("test.png"); GenerateMonochromeBitmap(pdfFilePath, bmpFilePath, 1280, 720); return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值