wxleasyland@139.com

2021.12

想要一个打印到图片的虚拟打印机软件,结果都是收费的。自已编是不可能的,工作量太大。

现在PDF虚拟打印机很多,可以打成PDF,再转成图片。

想要一个免费的简单的PDF转图片开源软件,结果不好找。

发现商用PDF SDK都是收费的,只好用PDF开源库来做了。

poppler开源库用于PDF浏览等,附带了一个pdftocairo可以转换pdf到图片的程序。

于是自己编译poppler和pdftocairo。

VS里,pdftocairo项目是依赖poppler项目的。

一、不要自己编译poppler的依赖库!

poppler项目是采用cmake来生成的,需先安装cmake。

cmake成功后会成生VC用的解决方案.sln,用VS打开它就可以了。

poppler主要是面向linux的,对WINDOWS不友好,它的依赖库相当多,而且有的依赖库又还有其它的依赖库,自己用VS编译的难度非常大!

从网上一个一个地下载开源依赖库,一个个编译,每个依赖库编译方法都不一样。

英文不好,工具不熟,花了大量精力,说多了都是泪,从入门到放弃,直接跳第二章。

在网上可以下载到各个WIN版生成好的依赖库,但都很老了,都是2011年以前的了,因为现在WIN下面可以方便地用vcpkg安装依赖库了,所以他们都没更新了。

网址:https://download-fallback.gnome.org/binaries/win32/dependencies/

这里记录了放弃的过程:

需要用VS2015及以上。VS2012不行,不支持新特性,一些库的程序在编译时会出错。

用cmake图形界面或命令行都可以。

出错:CMake Error at cmake/modules/PopplerMacros.cmake:91 (message): Unsupported CMAKE_BUILD_TYPE:

解决:

加上这个ENTRY:   CMAKE_BUILD_TYPE,值release,就可以(见《INSTALL》文件)

也可以CMakeLists.txt文件中加在project(poppler)下面:

SET(CMAKE_BUILD_TYPE  "release" CACHE STRING "")

出错:No test data found in $testdatadir.

解决:

去https://github.com/freedesktop/poppler-test下载,然后放在一个目录,然后加ENTRY,比如:

SET(TESTDATADIR  "C:/VC_PROJ/poppler-21.11.0/poppler-test-master" CACHE STRING "")

cairo库

还需要pixman库,gnumake.exe

freetype

release,静态库lib, unicode环境 ,/MD

libjpeg

release,静态库lib, unicode ,/MD

tiff

直接用下载的库

zlib

用nmake生成,zlib.lib是静态库/MD

libpng

release,静态库lib, 多字节,/MD

boost

要VS2015及以上,不然提示出错:只有静态常量整型数据成员才可以在类中初始化

于是用VS2017生成。

openjpeg

直接用官方生成好的OpenJPEG 2.3.1版x86

SET(OpenJPEG_DIR   "C:/VC_PROJ/openjpeg-v2.3.1-windows-x86/lib/openjpeg-2.3"   CACHE PATH "")

在这个目录中才有OpenJPEGConfig.cmake文件,才行。

pkg-config (用于帮助生成编译命令行选项)

libiconv

需要用cygwin。You also need the scripts 'ar-lib' and 'compile'。

到libiconv这里卡住了,虽然就快差不多了,但还要一些东东才行,太麻烦了,不想进行了,放弃,改用vcpkg install。

这时,自己修改补充的CMakeLists.txt文件内容如下:

cmake_minimum_required(VERSION 3.10.0 FATAL_ERROR)

project(poppler)

SET(CMAKE_BUILD_TYPE    "release"   CACHE STRING "")

SET(TESTDATADIR    "C:/VC_PROJ/poppler-21.11.0/poppler-test-master"   CACHE STRING "")

SET(FREETYPE_INCLUDE_DIR_freetype2   "C:/VC_PROJ/freetype-2.10.2/freetype-2.10.2/include/freetype"   CACHE PATH "")

SET(FREETYPE_INCLUDE_DIR_ft2build   "C:/VC_PROJ/freetype-2.10.2/freetype-2.10.2/include"   CACHE PATH "")

SET(FREETYPE_LIBRARY_DEBUG   "C:/VC_PROJ/freetype-2.10.2/freetype-2.10.2/objs/Win32/Release Static/freetype.lib"   CACHE FILEPATH "")

SET(FREETYPE_LIBRARY_RELEASE   "C:/VC_PROJ/freetype-2.10.2/freetype-2.10.2/objs/Win32/Release Static/freetype.lib"   CACHE FILEPATH "")

SET(JPEG_INCLUDE_DIR   "C:/VC_PROJ/libjpeg_VS2012"   CACHE PATH "")

SET(JPEG_LIBRARY_DEBUG   "C:/VC_PROJ/libjpeg_VS2012/Release/Win32/jpeg.lib"   CACHE FILEPATH "")

SET(JPEG_LIBRARY_RELEASE   "C:/VC_PROJ/libjpeg_VS2012/Release/Win32/jpeg.lib"   CACHE FILEPATH "")

SET(TIFF_INCLUDE_DIR   "C:/VC_PROJ/tiff-3.8.2-1-lib/include"   CACHE PATH "")

SET(TIFF_LIBRARY_DEBUG   "C:/VC_PROJ/tiff-3.8.2-1-lib/lib/libtiff.lib"   CACHE FILEPATH "")

SET(TIFF_LIBRARY_RELEASE   "C:/VC_PROJ/tiff-3.8.2-1-lib/lib/libtiff.lib"   CACHE FILEPATH "")

SET(ZLIB_INCLUDE_DIR   "C:/VC_PROJ/zlib-1.2.11"   CACHE PATH "")

SET(ZLIB_LIBRARY_DEBUG   "C:/VC_PROJ/zlib-1.2.11/zlib.lib"   CACHE FILEPATH "")

SET(ZLIB_LIBRARY_RELEASE   "C:/VC_PROJ/zlib-1.2.11/zlib.lib"   CACHE FILEPATH "")

SET(PNG_PNG_INCLUDE_DIR   "C:/VC_PROJ/libpng-1.6.37"   CACHE PATH "")

SET(PNG_LIBRARY_DEBUG   "C:/VC_PROJ/libpng-1.6.37/projects/vstudio/Release Library/libpng16.lib"   CACHE FILEPATH "")

SET(PNG_LIBRARY_RELEASE   "C:/VC_PROJ/libpng-1.6.37/projects/vstudio/Release Library/libpng16.lib"   CACHE FILEPATH "")

SET(OpenJPEG_DIR   "C:/VC_PROJ/openjpeg-v2.3.1-windows-x86/lib/openjpeg-2.3"   CACHE PATH "")

#SET(ENABLE_BOOST   FALSE  CACHE BOOL  "")

SET(CAIRO_INCLUDE_DIR   "C:/VC_PROJ/cairo-1.17.4/src"   CACHE PATH "")

SET(CAIRO_LIBRARY   "C:/VC_PROJ/cairo-1.17.4/src/release/cairo-static.lib"   CACHE FILEPATH "")

SET(Boost_INCLUDE_DIR   "C:/VC_PROJ/boost_1_77_0"   CACHE PATH "")

SET(Boost_DIR   "C:/VC_PROJ/boost_1_77_0"   CACHE PATH "")

SET(PKG_CONFIG_EXECUTABLE   "C:/VC_PROJ/pkg-config-0.29.2/release/Win32/pkg-config.exe"   CACHE FILEPATH "")

二、直接用vcpkg下载编译poppler的依赖库!

都2021年了,现在WINDOWS下都是用vcpkg来安装各个开源库了,非常方便!大家不要自己去下载和编译了!

vcpkg需要VS2015及以上。需要有英文语言VS。

VCPKG带有poppler库,你要用直接用它也可以,我没有用。

我是下载POPPLER开源库,而依赖库用VCPKG下载编译的!!

可以用VCPKG安装poppler,这样它的依赖库都会一并装好了!

vcpkg install poppler:x86-windows

vcpkg install curl:x86-windows

VCPKG install安装时有的文件不好下载,可以自己另外下载后改成目标文件名,放到downloads目录中,再次vcpkg install,即马上成功。

然后会有cmake可以用的toolchain文件,在 C:/VC_PROJ/vcpkg-master/scripts/buildsystems/vcpkg.cmake,有它的话,cmake可以找到所有vcpkg安装的库!太方便了。

所以,一定要在poppler库的cmakelists.txt文件第一行(首行)加上:

set(CMAKE_TOOLCHAIN_FILE "C:/VC_PROJ/vcpkg-master/scripts/buildsystems/vcpkg.cmake"  )

注意是:一定是首行,set是小写,这样cmake才会知道这个toolchain!

其它自己加的ENTRY放在“project(poppler)”后面:

SET(CMAKE_BUILD_TYPE    "release"   CACHE STRING "")

SET(TESTDATADIR    "C:/VC_PROJ/poppler-21.11.0/poppler-test-master"   CACHE STRING "")

SET(PKG_CONFIG_EXECUTABLE   "C:/VC_PROJ/pkg-config-0.29.2/release/Win32/pkg-config.exe"   CACHE FILEPATH "")

需要pkg-config,可以是VCPKG下载的或自己编译的。

最后修改后的cmakelists.txt文件内容为:

set(CMAKE_TOOLCHAIN_FILE "C:/VC_PROJ/vcpkg-master/scripts/buildsystems/vcpkg.cmake"  )

cmake_minimum_required(VERSION 3.10.0 FATAL_ERROR)

project(poppler)

SET(CMAKE_BUILD_TYPE    "release"   CACHE STRING "")

SET(TESTDATADIR    "C:/VC_PROJ/poppler-21.11.0/poppler-test-master"   CACHE STRING "")

#SET(PKG_CONFIG_EXECUTABLE   "C:/VC_PROJ/pkg-config-0.29.2/release/Win32/pkg-config.exe"   CACHE FILEPATH "")

SET(PKG_CONFIG_EXECUTABLE   "C:/VC_PROJ/vcpkg-master/installed/x86-windows/tools/pkgconf/pkgconf.exe"   CACHE FILEPATH "")

然后cmake成功,提示信息如下:

Selecting Windows SDK version 10.0.17763.0 to target Windows 10.0.19043.

The C compiler identification is MSVC 19.16.27045.0

The CXX compiler identification is MSVC 19.16.27045.0

Detecting C compiler ABI info

Detecting C compiler ABI info - done

Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe - skipped

Detecting C compile features

Detecting C compile features - done

Detecting CXX compiler ABI info

Detecting CXX compiler ABI info - done

Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe - skipped

Detecting CXX compile features

Detecting CXX compile features - done

Found PkgConfig: C:/VC_PROJ/vcpkg-master/installed/x86-windows/tools/pkgconf/pkgconf.exe (found version "1.8.0")

-- Using win32 threads

Checking _FILE_OFFSET_BITS for large files

Checking _FILE_OFFSET_BITS for large files - not needed

Found Freetype: optimized;C:/VC_PROJ/vcpkg-master/installed/x86-windows/lib/freetype.lib;debug;C:/VC_PROJ/vcpkg-master/installed/x86-windows/debug/lib/freetyped.lib (found version "2.11.0")

Found JPEG: optimized;C:/VC_PROJ/vcpkg-master/installed/x86-windows/lib/jpeg.lib;debug;C:/VC_PROJ/vcpkg-master/installed/x86-windows/debug/lib/jpeg.lib (found version "62")

Found ZLIB: optimized;C:/VC_PROJ/vcpkg-master/installed/x86-windows/lib/zlib.lib;debug;C:/VC_PROJ/vcpkg-master/installed/x86-windows/debug/lib/zlibd.lib (found version "1.2.11")

Found PNG: optimized;C:/VC_PROJ/vcpkg-master/installed/x86-windows/lib/libpng16.lib;debug;C:/VC_PROJ/vcpkg-master/installed/x86-windows/debug/lib/libpng16d.lib (found version "1.6.37")

Found TIFF: optimized;C:/VC_PROJ/vcpkg-master/installed/x86-windows/lib/tiff.lib;debug;C:/VC_PROJ/vcpkg-master/installed/x86-windows/debug/lib/tiffd.lib (found version "4.3.0")

CMake Warning at C:/VC_PROJ/vcpkg-master/scripts/buildsystems/vcpkg.cmake:788 (_find_package):

By not providing "FindQt5Core.cmake" in CMAKE_MODULE_PATH this project has

asked CMake to find a package configuration file provided by "Qt5Core", but

CMake did not find one.

Could not find a package configuration file provided by "Qt5Core"

(requested version 5.9) with any of the following names:

Qt5CoreConfig.cmake

qt5core-config.cmake

Add the installation prefix of "Qt5Core" to CMAKE_PREFIX_PATH or set

"Qt5Core_DIR" to a directory containing one of the above files. If

"Qt5Core" provides a separate development package or SDK, be sure it has

been installed.

Call Stack (most recent call first):

CMakeLists.txt:197 (find_package)

CMake Warning at C:/VC_PROJ/vcpkg-master/scripts/buildsystems/vcpkg.cmake:788 (_find_package):

By not providing "FindQt5Gui.cmake" in CMAKE_MODULE_PATH this project has

asked CMake to find a package configuration file provided by "Qt5Gui", but

CMake did not find one.

Could not find a package configuration file provided by "Qt5Gui" with any

of the following names:

Qt5GuiConfig.cmake

qt5gui-config.cmake

Add the installation prefix of "Qt5Gui" to CMAKE_PREFIX_PATH or set

"Qt5Gui_DIR" to a directory containing one of the above files. If "Qt5Gui"

provides a separate development package or SDK, be sure it has been

installed.

Call Stack (most recent call first):

CMakeLists.txt:198 (find_package)

CMake Warning at C:/VC_PROJ/vcpkg-master/scripts/buildsystems/vcpkg.cmake:788 (_find_package):

By not providing "FindQt5Xml.cmake" in CMAKE_MODULE_PATH this project has

asked CMake to find a package configuration file provided by "Qt5Xml", but

CMake did not find one.

Could not find a package configuration file provided by "Qt5Xml" with any

of the following names:

Qt5XmlConfig.cmake

qt5xml-config.cmake

Add the installation prefix of "Qt5Xml" to CMAKE_PREFIX_PATH or set

"Qt5Xml_DIR" to a directory containing one of the above files. If "Qt5Xml"

provides a separate development package or SDK, be sure it has been

installed.

Call Stack (most recent call first):

CMakeLists.txt:199 (find_package)

CMake Warning at C:/VC_PROJ/vcpkg-master/scripts/buildsystems/vcpkg.cmake:788 (_find_package):

By not providing "FindQt5Widgets.cmake" in CMAKE_MODULE_PATH this project

has asked CMake to find a package configuration file provided by

"Qt5Widgets", but CMake did not find one.

Could not find a package configuration file provided by "Qt5Widgets" with

any of the following names:

Qt5WidgetsConfig.cmake

qt5widgets-config.cmake

Add the installation prefix of "Qt5Widgets" to CMAKE_PREFIX_PATH or set

"Qt5Widgets_DIR" to a directory containing one of the above files. If

"Qt5Widgets" provides a separate development package or SDK, be sure it has

been installed.

Call Stack (most recent call first):

CMakeLists.txt:200 (find_package)

CMake Warning at C:/VC_PROJ/vcpkg-master/scripts/buildsystems/vcpkg.cmake:788 (_find_package):

By not providing "FindQt5Test.cmake" in CMAKE_MODULE_PATH this project has

asked CMake to find a package configuration file provided by "Qt5Test", but

CMake did not find one.

Could not find a package configuration file provided by "Qt5Test" with any

of the following names:

Qt5TestConfig.cmake

qt5test-config.cmake

Add the installation prefix of "Qt5Test" to CMAKE_PREFIX_PATH or set

"Qt5Test_DIR" to a directory containing one of the above files. If

"Qt5Test" provides a separate development package or SDK, be sure it has

been installed.

Call Stack (most recent call first):

CMakeLists.txt:201 (find_package)

-- Package Qt5Core or Qt5Gui or Qt5Xml or Qt5Widgets or Qt5Test not found

-- Package Qt6Core or Qt6Gui or Qt6Widgets or Qt6Test not found

Found Cairo: C:/VC_PROJ/vcpkg-master/installed/x86-windows/debug/lib/cairo.lib (Required is at least version "1.10.0")

Found Boost: C:/VC_PROJ/vcpkg-master/installed/x86-windows/include (found suitable version "1.77.0", minimum required is "1.58.0")

Checking for modules 'glib-2.0>=2.56;gobject-2.0>=2.56;gio-2.0>=2.56'

Package 'glib-2.0', required by 'virtual:world', not found

Package 'gobject-2.0', required by 'virtual:world', not found

Package 'gio-2.0', required by 'virtual:world', not found

Could NOT find GLIB (missing: GLIB2_LIBRARIES GLIB2_CFLAGS)

Performing Test ICONV_SECOND_ARGUMENT_IS_CONST

Performing Test ICONV_SECOND_ARGUMENT_IS_CONST - Failed

Found Iconv: C:/VC_PROJ/vcpkg-master/installed/x86-windows/debug/lib/iconv.lib

Found lcms version 2.12, C:/VC_PROJ/vcpkg-master/installed/x86-windows/debug/lib/lcms2.lib

Found ZLIB: optimized;C:/VC_PROJ/vcpkg-master/installed/x86-windows/lib/zlib.lib;debug;C:/VC_PROJ/vcpkg-master/installed/x86-windows/debug/lib/zlibd.lib (found suitable version "1.2.11", minimum required is "1")

Checking for module 'poppler-data'

Package 'poppler-data', required by 'virtual:world', not found

Looking for include file dlfcn.h

Looking for include file dlfcn.h - not found

Looking for include file fcntl.h

Looking for include file fcntl.h - found

Looking for include file stdlib.h

Looking for include file stdlib.h - found

Looking for include file sys/mman.h

Looking for include file sys/mman.h - not found

Looking for include file sys/stat.h

Looking for include file sys/stat.h - found

Looking for include file unistd.h

Looking for include file unistd.h - not found

Looking for fseek64

Looking for fseek64 - not found

Looking for fseeko

Looking for fseeko - not found

Looking for ftell64

Looking for ftell64 - not found

Looking for pread64

Looking for pread64 - not found

Looking for lseek64

Looking for lseek64 - not found

Looking for gmtime_r

Looking for gmtime_r - not found

Looking for timegm

Looking for timegm - not found

Looking for gettimeofday

Looking for gettimeofday - not found

Looking for localtime_r

Looking for localtime_r - not found

Looking for popen

Looking for popen - not found

Looking for mkstemp

Looking for mkstemp - not found

Looking for strtok_r

Looking for strtok_r - not found

Performing Test HAVE_DIRENT_H

Performing Test HAVE_DIRENT_H - Failed

Performing Test HAVE_NDIR_H

Performing Test HAVE_NDIR_H - Failed

Performing Test HAVE_SYS_DIR_H

Performing Test HAVE_SYS_DIR_H - Failed

Performing Test HAVE_SYS_NDIR_H

Performing Test HAVE_SYS_NDIR_H - Failed

Looking for nanosleep

Looking for nanosleep - not found

Looking for nanosleep in rt

Looking for nanosleep in rt - not found

Performing Test COMPILER_HAS_DEPRECATED_ATTR

Performing Test COMPILER_HAS_DEPRECATED_ATTR - Failed

Performing Test COMPILER_HAS_DEPRECATED

Performing Test COMPILER_HAS_DEPRECATED - Success

Building Poppler with support for:

font configuration: win32

use boost (Splash): yes

cairo output: yes

qt5 wrapper: no

qt6 wrapper: no

glib wrapper: no

introspection: no

gtk-doc: no

cpp wrapper: yes

use libjpeg: yes

use libpng: yes

use libtiff: yes

use zlib compress: yes

use zlib uncompress: no

use nss3: no

use curl: yes

use libopenjpeg2: yes

use lcms2: yes

command line utils: yes

fuzz target: no

test data dir: C:/VC_PROJ/poppler-21.11.0/poppler-test-master

Configuring done

自动生成的ENTRY是这样的:依赖库的include目录在C:/VC_PROJ/vcpkg-master/installed/x86-windows/include

VS2017打开项目方案.sln,编译成功!

生成pdftocairo.exe等文件在C:\VC_PROJ\poppler-21.11.0\poppler-21.11.0\build\utils\Debug。

pdftocairo 可以运行,但需要多个依赖库的dll文件才行!

pdftocairo是依赖了poppler静态库,但其它的依赖库都是dll。

三、用静态库来生成pdftocairo

(后注:网友说在cmakelists.txt文件中加上相关项就可以使用vcpkg的静态依赖库了:

在project命令前设置:

set(VCPKG_TARGET_TRIPLET "x86-windows-static" CACHE STRING "" FORCE)

set(CMAKE_TOOLCHAIN_FILE "vcpkg\\scripts\\buildsystems\\vcpkg.cmake")

我没有试了。本章就算看看热闹吧。

尝试用静态库来生成pdftocairo.exe,就不需要各个dll文件了。

先安装依赖库,比如:

vcpkg install poppler:x86-windows-static

这样相关静态依赖库都会安装好。(静态依赖库和上面的动态依赖库是分开放的)

curl单独安装:

vcpkg install curl:x86-windows-static

再在VS2017的pdftocairo项目里,把“链接器”选项的依赖库,改成static目录里的:

比如:

原来是C:\VC_PROJ\vcpkg-master\installed\x86-windows\lib\cairo.lib

改成:

C:\VC_PROJ\vcpkg-master\installed\x86-windows-static\lib\cairo.lib

结果编译不行,提示很多错,比如:

错误LNK2019无法解析的外部符号 __imp__cairo_create,该符号在函数 "void __cdecl renderPage(class PDFDoc *,class CairoOutputDev *,int,double,double,double,double)" (?renderPage@@YAXPAVPDFDoc@@PAVCairoOutputDev@@HNNNN@Z) 中被引用        C:\VC_PROJ\poppler-21.11.0\poppler-21.11.0\build\utils\pdftocairo.obj

即找不到__imp__cairo_create这个函数符号。

静态库看dumpbin /linkermember cairo.lib,看到有_cairo_create,但没有__imp__cairo_create

动态库看dumpbin /linkermember cairo.lib,看到有__imp__cairo_create,也有_cairo_create

pdftocairo是引用了__imp__cairo_create,而不是引用_cairo_create,所以用静态库就找不到函数了。

发现cairo.h静态库和动态库的头文件不一样,在不同目录。

静态库cairo.h:

#ifndef cairo_public

# if defined (_MSC_VER) && ! 1

#  define cairo_public __declspec(dllimport)

# else

#  define cairo_public

# endif

#endif

动态库cairo.h:

#ifndef cairo_public

# if defined (_MSC_VER) && ! 0

#  define cairo_public __declspec(dllimport)

# else

#  define cairo_public

# endif

#endif

所以头文件会影响静态库、动态库的函数名称,应该要包含“静态库目录”的头文件!

在VS2017的pdftocairo项目里,把“C/C++”、“常规”选项的包含目录,改成static目录里的。

改成

C:\VC_PROJ\vcpkg-master\installed\x86-windows-static\include

C:\VC_PROJ\vcpkg-master\installed\x86-windows-static\include\cairo

编译,还提示很多错,比如:

错误LNK2019无法解析的外部符号 _pixman_format_supported_destination,该符号在函数 __pixman_format_from_masks 中被引用    pdftocairo C:\VC_PROJ\poppler-21.11.0\poppler-21.11.0\build\utils\cairo.lib(cairo-image-surface.c.obj)

解决:

因为缺少依赖库本身的依赖库,比如cairo静态库有了,但cairo静态库本身还需要pixman静态库,还需要把pixman静态库lib搞进来。如果cairo是运态库dll,就没有这个烦恼了。所以静态库项目比较麻烦。

VS链接器选项中加入依赖库:

C:\VC_PROJ\vcpkg-master\installed\x86-windows-static\lib\pixman-1.lib

错误LNK2019无法解析的外部符号 __imp__GradientFill@24,该符号在函数 __cairo_win32_printing_surface_paint_linear_pattern 中被引用   pdftocairo    C:\VC_PROJ\poppler-21.11.0\poppler-21.11.0\build\utils\cairo.lib(win32_cairo-win32-printing-surface.c.obj)

解决:GradientFill是WINDOWS系统的Msimg32.lib,将它加入链接器选项中,直接写Msimg32.lib即可,OK。

错误LNK2019无法解析的外部符号 _FcConfigGetCurrent,该符号在函数 __cairo_ft_font_face_get_implementation 中被引用 pdftocairo C:\VC_PROJ\poppler-21.11.0\poppler-21.11.0\build\utils\cairo.lib(cairo-ft-font.c.obj)

解决:

经findstr在lib文件中查找FcConfigGetCurrent,发现该函数是在fontconfig.lib中。加入C:\VC_PROJ\vcpkg-master\installed\x86-windows-static\lib\fontconfig.lib

错误LNK2019无法解析的外部符号 _BrotliDecoderDecompress,该符号在函数 _woff2_decompress 中被引用  pdftocairo C:\VC_PROJ\poppler-21.11.0\poppler-21.11.0\build\utils\freetype.lib(sfnt.c.obj)

解决:加入C:\VC_PROJ\vcpkg-master\installed\x86-windows-static\lib\brotlidec-static.lib

错误LNK2019无法解析的外部符号 _BZ2_bzDecompressInit,该符号在函数 _FT_Stream_OpenBzip2 中被引用   pdftocairo C:\VC_PROJ\poppler-21.11.0\poppler-21.11.0\build\utils\freetype.lib(ftbzip2.c.obj)

解决:加入C:\VC_PROJ\vcpkg-master\installed\x86-windows-static\lib\bz2.lib

错误LNK2019无法解析的外部符号 __imp__cmsGetEncodedCMMversion@0,该符号在函数 "public: char * __thiscall GfxICCBasedColorSpace::getPostScriptCSA(void)" (?getPostScriptCSA@GfxICCBasedColorSpace@@QAEPADXZ) 中被引用  pdftocairo    C:\VC_PROJ\poppler-21.11.0\poppler-21.11.0\build\utils\poppler.lib(GfxState.obj)

解决:

因为poppler库中调用的是lcms的DLL库,不是lcms静态库,还要修改poppler库项目。

改poppler库项目的包含目录,从C:\VC_PROJ\vcpkg-master\installed\x86-windows\include,改成C:\VC_PROJ\vcpkg-master\installed\x86-windows-static\include。

再重新生成。

错误LNK2019无法解析的外部符号 _BrotliTransformDictionaryWord,该符号在函数 _ProcessCommands 中被引用   pdftocairo C:\VC_PROJ\poppler-21.11.0\poppler-21.11.0\build\utils\brotlidec-static.lib(decode.c.obj)

解决:加入C:\VC_PROJ\vcpkg-master\installed\x86-windows-static\lib\brotlicommon-static.lib

错误LNK2019无法解析的外部符号 _XML_ParserCreate,该符号在函数 _FcConfigParseAndLoadFromMemoryInternal 中被引用 pdftocairo C:\VC_PROJ\poppler-21.11.0\poppler-21.11.0\build\utils\fontconfig.lib(fcxml.c.obj)   

解决:加入C:\VC_PROJ\vcpkg-master\installed\x86-windows-static\lib\libexpatMD.lib

错误LNK2019无法解析的外部符号 _lzma_code,该符号在函数 _LZMADecode 中被引用    pdftocairo C:\VC_PROJ\poppler-21.11.0\poppler-21.11.0\build\utils\tiff.lib(tif_lzma.c.obj) 

解决:加入C:\VC_PROJ\vcpkg-master\installed\x86-windows-static\lib\lzma.lib

最终成功:

生成了C:\VC_PROJ\poppler-21.11.0\poppler-21.11.0\build\utils\Release\pdftocairo.exe

运行OK,不需要那些dll文件了。

VS2017的pdftocairo项目中,“链接器”是这样的设置:

C:\VC_PROJ\vcpkg-master\installed\x86-windows-static\lib\lzma.lib

C:\VC_PROJ\vcpkg-master\installed\x86-windows-static\lib\libexpatMD.lib

C:\VC_PROJ\vcpkg-master\installed\x86-windows-static\lib\brotlicommon-static.lib

C:\VC_PROJ\vcpkg-master\installed\x86-windows-static\lib\bz2.lib

C:\VC_PROJ\vcpkg-master\installed\x86-windows-static\lib\brotlidec-static.lib

C:\VC_PROJ\vcpkg-master\installed\x86-windows-static\lib\fontconfig.lib

C:\VC_PROJ\vcpkg-master\installed\x86-windows-static\lib\cairo.lib

C:\VC_PROJ\vcpkg-master\installed\x86-windows-static\lib\pixman-1.lib

C:\VC_PROJ\vcpkg-master\installed\x86-windows-static\lib\freetype.lib

C:\VC_PROJ\vcpkg-master\installed\x86-windows-static\lib\lcms2.lib

C:\VC_PROJ\vcpkg-master\installed\x86-windows-static\lib\jpeg.lib

C:\VC_PROJ\vcpkg-master\installed\x86-windows-static\lib\libcurl.lib

C:\VC_PROJ\vcpkg-master\installed\x86-windows-static\lib\openjp2.lib

C:\VC_PROJ\vcpkg-master\installed\x86-windows-static\lib\libpng16.lib

C:\VC_PROJ\vcpkg-master\installed\x86-windows-static\lib\zlib.lib

C:\VC_PROJ\vcpkg-master\installed\x86-windows-static\lib\tiff.lib

..\Release\poppler.lib

winmm.lib

ws2_32.lib

advapi32.lib

crypt32.lib

gdi32.lib

kernel32.lib

user32.lib

winspool.lib

shell32.lib

ole32.lib

oleaut32.lib

uuid.lib

comdlg32.lib

Msimg32.lib

四、pdftocairo的文件中文名问题修正

pdftocairo.exe,如果输出文件名是有中文,则会出错!

比如  pdftocairo -png  我.pdf

提示Error opening output file 鎴?

如果输出文件名没有中文,则OK,没问题。   比如   pdftocairo -png  我.pdf   aaa

poppler是按UTF8文件名打开文件的,见别人说的:

poppler的windows实现中,打开文件经过了一次文件名从utf8到utf16的转码。

  // try to open file

#ifdef _WIN32

  wchar_t *wFileName = (wchar_t*)utf8ToUtf16(fileName->getCString());

  file = GooFile::open(wFileName);

  gfree(wFileName);

#else

因此,这就要求我们输入文件名给poppler也是使用utf8编码的。

pdftocairo会按unicode读入命令行,再转化成utf8,以方便poppler打开。

所以不是poppler的问题,是pdftocairo处理输出文件名时,有bug。

分析一下pdftocairo项目程序pdftocairo.cc:

文件名称会存放在GooString类中。

找到是writePageImage()这个函数中出错提示Error opening output file。

输出到PNG\JPG\TIFF图片,则printing变量是0,不算是printing。输出到PS\EPS\PDF\SVG或打印机,就算是printing。

printToWin32 是表示打印到windows打印机。

主程序循环中调用:beginDocument,beginPage,renderPage,endPage。

beginDocument()中,如果是printing,则有打开fopen输出文件。

endPage()中,如果不是printing,则调用→writePageImage()中有打开fopen输出文件。

都是因为采用UTF8文件名来fopen,所以出错了,需要用多字节字符串才行。

把这二个地方都改一下:

writePageImage()中:

if (filename->cmp("fd://0") == 0) {

#if defined(_WIN32) || defined(__CYGWIN__)

        _setmode(fileno(stdout), O_BINARY);

#endif

        file = stdout;

    } else

      {

        //经测试,这里filename->c_str()是UTF8字串,所以fopen会出错,需转换成windows的多字节字串才行。

        file = 0;

      // UTF8转unicode宽字符

        char wbuff[4000];

        int wi;

        wi = MultiByteToWideChar(CP_UTF8, 0, filename->c_str(), -1, (LPWSTR)wbuff, 4000);

        wbuff[wi * 2] = 0;

        wbuff[wi * 2 + 1] = 0;

        if (wi == 0)

            goto WOUT;

      //unicode宽字符转多字节ANSI

        char wbuff2[4000];

        wi = WideCharToMultiByte(CP_ACP, NULL,(LPCWCH) wbuff, -1, wbuff2, 4000, 0, 0);

        wbuff2[wi] = 0;

        if (wi == 0)

            goto WOUT;

      file = fopen(wbuff2, "wb");

      //file = fopen(filename->c_str(), "wb");

      WOUT:;

      }

    if (!file) {

        fprintf(stderr, "FUNCTION writePageImage() Error opening output file %s\n", filename->c_str());

        exit(2);

    }

beginDocument()中:

if (outputFileName->cmp("fd://0") == 0) {

#if defined(_WIN32) || defined(__CYGWIN__)

                _setmode(fileno(stdout), O_BINARY);

#endif

                output_file = stdout;

            }

          else

          {

               

                output_file = 0;

                // UTF8转unicode宽字符

                char wbuff[4000];

                int wi;

                wi = MultiByteToWideChar(CP_UTF8, 0, outputFileName->c_str(), -1, (LPWSTR)wbuff, 4000);

                wbuff[wi * 2] = 0;

                wbuff[wi * 2 + 1] = 0;

                if (wi == 0)

                    goto WbeginDocumentOUT;

                // unicode宽字符转多字节ANSI

                char wbuff2[4000];

                wi = WideCharToMultiByte(CP_ACP, NULL, (LPCWCH)wbuff, -1, wbuff2, 4000, 0, 0);

                wbuff2[wi] = 0;

                if (wi == 0)

                    goto WbeginDocumentOUT;

              output_file = fopen(wbuff2,   "wb");

              //output_file = fopen(outputFileName->c_str(), "wb");

          WbeginDocumentOUT:

                if (!output_file)

              {

                    fprintf(stderr, "FUNCGION beginDocument() Error opening output file %s\n", outputFileName->c_str());

                    exit(2);

                }

            }

成功。

五、poppler-data库

poppler-data说是可以让poppler正确地渲染CJK中日韩文字。

但应该是linux中才需要,没找到在win中的安装方法,不知道怎么安装。不用了。实际中文显示正常。

六、成品软件下载

pdftocairo.exe是命令行的,使用也很简单。

https://download.csdn.net/download/wxlfreewind/54145551

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐