PDFHummus在VS中运行正常,单独执行EXE插入PDF图片时文件损坏
使用PDFHummus插入PDF矢量图导致单独运行EXE生成损坏PDF的排查方案
问题背景
使用PDFHummus生成包含PDF矢量图的PDF文件,已将库文件和DLL放置在主目录,Visual Studio中配置好链接器与包含目录后,Release模式下直接运行程序一切正常,但单独执行生成的EXE时,虽能创建PDF文件但文件已损坏。仅保留文本内容、插入PNG图片时,EXE运行均正常;仅插入PDF矢量图时出现问题,推测为工作目录缺失特定DLL。
测试代码
#include <iostream> #include <string> // end standard library includes // pdfwriter library includes #include "PDFWriter.h" #include "PDFPage.h" #include "PageContentContext.h" #include "PDFFormXObject.h" #include "ResourcesDictionary.h" // end pdfwriter library includes using namespace PDFHummus; int main(int argc, char** const argv) { // Step 1: Create a PDFWriter instance PDFWriter pdfWriter; if (pdfWriter.StartPDF("HelloWorld.pdf", ePDFVersion13) != PDFHummus::eSuccess) return; PDFPage* page = new PDFPage(); page->SetMediaBox(PDFRectangle(0, 0, 791, 612)); PageContentContext* cxt = pdfWriter.StartPageContentContext(page); cxt->DrawImage(300, 570, "avent3.pdf"); // cxt->DrawImage(300, 570, "avent3.png"); PDFUsedFont* font = pdfWriter.GetFontForFile("times.ttf"); AbstractContentContext::TextOptions textOptions(font, 14, AbstractContentContext::eGray, 0); cxt->WriteText(30, 570, "Hello World", textOptions); pdfWriter.EndPageContentContext(cxt); pdfWriter.WritePage(page); delete page; // Step 2 Close the PDF pdfWriter.EndPDF(); return 0; }
排查与解决思路
- 确认PDF解析依赖DLL:PDFHummus处理PDF嵌入依赖
libpodofo.dll(或对应版本的Podofo相关库),该DLL是解析外部PDF文件的核心依赖,需将其复制到EXE所在目录。 - 用工具检测缺失DLL:使用Dependency Walker或VS自带的
dumpbin /dependents <你的EXE路径>命令,扫描EXE的运行时依赖项,定位未部署的缺失库。 - 验证文件路径有效性:单独运行EXE时,确保
avent3.pdf与EXE在同一目录,或在代码中使用绝对路径加载该文件——路径错误会导致写入不完整的损坏PDF。 - 完整部署所有运行时依赖:除PDFHummus主库DLL外,还需复制VC++运行时库(如
msvcp140.dll、vcruntime140.dll,依VS版本而定)到EXE目录,VS运行时会自动加载这些库,但单独执行时需手动部署。 - 添加错误日志定位问题:修改代码,在
StartPDF失败或DrawImage操作后,打印pdfWriter.GetLastError()获取具体错误信息,直接定位是DLL缺失还是文件读取失败。
内容的提问来源于stack exchange,提问作者Diego Fernando Pava
相关产品推荐
相关产品推荐

