
我已经使用IOS将图片导出到相册,而今天,我已经开发了一种用于IOS导出pdf文件的方法. 百度有很多. 许多网站都转载了本文“如何在iOS中使用HTML模板和UIPrintPageRenderer生成PDF文档”,但是本文的解决方案是通过在应用程序中使用html来实现的. 这是一个解决方案,但对于应用程序仅为此功能嵌套网页而言,这有点痛苦. 搜索后,我觉得更好的解决方案是通过libharu创建pdf文件.
libharu是C语言跨平台,开源和免费pdf生成库. 它可以将文本,图片等生成为pdf文件. 它还依赖Libpng库. 通过这两个库,可以生成pdf文档.
在C ++中也有PDFLib,因为PDFLib库对个人免费,并且商业产品需要购买许可证,因此首选libharu.
libharu官方网站地址:
libharu的Github下载地址:
libpng的Github下载地址:
以下三个选项是可选的,我的演示使用第三个选项.
在INSTALL中说明了官方推荐的方法. 下载后,通过脚本命令./configure && make && make install进行安装. 如果通过git下载它,没有配置文件,则需要运行./安装后安装了buildconf.sh命令ios pdf 用什么打开,可以参考官方教程: 但是当我安装时,mac会提示此错误
make:?aclocal:?No?such?file?or?directory
make:?***?[aclocal.m4]?Error?1
我懒得检查它,所以我没有使用官方推荐的方法,但是应该可以使用官方推荐.
这是通过Homebrew安装的,如果您在计算机上安装了Homebrew,则可以直接使用
brew?install?libharu

此命令(如果未安装)可以在本文“使用终端在Mac上注销ipa软件包”中检查Homebrew的安装方法.

此安装是直接生成的库. 不建议使用此安装. 安装的库没有开源文件,使用起来很舒服.
此下载完成后,将libharu和libpng库中所有文件导入下载路径/ usr / local / Cellar中. 如果运行错误,请注意该错误,删除两个专门针对Mac(不适用于手机)的错误. 那两个图书馆
1. 删除从github下载的libharu文件夹下的包含文件中的Makefile.am,然后将win32 / include / hpdf_config.h文件复制到include文件夹中,然后将整个include文件夹放入项目中.
p>
2. 删除src文件夹中的Makefile.amios pdf 用什么打开,然后将整个src文件夹导入项目中
1. 将从github下载的libpng最外层文件夹中的所有.c和.h文件导入到项目中,除了pngtest.c
2,将scripts文件夹中的pnglibconf.h.prebuilt文件名修改为pnglibconf.h,并将其引入项目中
1. 介绍libz.tbd
2,介绍CoreGraphics.framework
1. libpng中报告了错误

"_png_init_filter_functions_neon",?referenced?from:
此错误的原因是NEON受限制. 解决方案是修改pngpriv.h文件的128至136行:
#??ifdef?__ARM_NEON__
#?????define?PNG_ARM_NEON_OPT?2
#??else
#?????define?PNG_ARM_NEON_OPT?0
#??endif
都修改为
#define?PNG_ARM_NEON_OPT?0
2,在libpng中报告了一个错误
Undefined?symbols?for?architecture?x86_64:
??"_adler32",?referenced?from:
??????_png_icc_set_sRGB?in?libpng.a(png.o)
??"_crc32",?referenced?from:
??????_png_reset_crc?in?libpng.a(png.o)
??????_png_calculate_crc?in?libpng.a(png.o)
??????_png_icc_set_sRGB?in?libpng.a(png.o)
这种错误,您需要注意是否引入了系统库libz.tbd
3. 如果您在编写的PDFService.h文件中报告错误
arc?forbids?objective-c?objects?in?struct
需要添加__unsafe_unretained关键字
typedef?struct?_PDFService_userData?{
????HPDF_Doc?pdf;
????__unsafe_unretained?PDFService?*service;
????__unsafe_unretained?NSString?*filePath;
}?PDFService_userData;

4,libpng报告错误
CgBI:?unhandled?critical?chunk
这是由于在pdf中生成图片的问题. 您可以尝试在构建设置中将“压缩PNG图片”和“从PNG文件中删除文本元数据”两个选项设置为“否”.
5. libharu报告未找到错误
这主要是libpng库没有正确引入
您可以检查
介绍hpdf.h头文件并创建设置参数
-?(void)createPDFFile:(NSString?*)filePath
{
????//?Creates?a?test?PDF?file?to?the?specified?path.
????//?TODO:?use?UIImage?to?create?non-optimized?PNG?rather?than?build?target?setting
????NSString?*path?=?nil;
????const?char?*pathCString?=?NULL;
????LOG(@"[libharu]?PDF?Creation?START");
????PDFService_userData?userData;
????HPDF_Doc?pdf?=?HPDF_New(PDFService_defaultErrorHandler,?&userData);
????userData.pdf?=?pdf;
????userData.service?=?self;
????userData.filePath?=?filePath;
????LOG(@"[libharu]?Adding?page?1");
????HPDF_Page?page1?=?HPDF_AddPage(pdf);
????LOG(@"[libharu]?SetSize?page?1");
????HPDF_Page_SetSize(page1,?HPDF_PAGE_SIZE_A4,?HPDF_PAGE_LANDSCAPE);
????LOG(@"[libharu]?TextOut?page?1");
????HPDF_Page_BeginText(page1);
????//英文
????HPDF_Font?fontEn?=?HPDF_GetFont(pdf,?"Helvetica",?"StandardEncoding");
????//中文
????HPDF_UseCNSFonts(pdf);
????HPDF_UseCNSEncodings(pdf);
????HPDF_Font?fontCH?=?HPDF_GetFont(pdf,?"SimSun",?"GBK-EUC-H");
????//日文
????HPDF_UseJPFonts?(pdf);
????HPDF_UseJPEncodings?(pdf);
????HPDF_Font?fontJP?=?HPDF_GetFont(pdf,?"MS-PGothic",?"90ms-RKSJ-H");
????HPDF_Page_SetFontAndSize(page1,?fontEn,?16.0);
????HPDF_Page_TextOut(page1,?50.00,?500.00,?"Hello?libHaru!");
????HPDF_Page_SetFontAndSize(page1,?fontCH,?16.0);
????HPDF_Page_TextOut(page1,?50.00,?460.00,?[@"胡东东博客"?cStringUsingEncoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000)]);
????HPDF_Page_SetFontAndSize(page1,?fontJP,?16.0);
????HPDF_Page_TextOut(page1,?50.00,?420.00,?[@"はろー日本語"?cStringUsingEncoding:NSShiftJISStringEncoding]);
????HPDF_Page_EndText(page1);
????LOG(@"[libharu]?Path?drawing?page?1");
????HPDF_Page_SetLineWidth(page1,?4.0);
????HPDF_Page_SetRGBStroke(page1,?1.0,?0,?0);
????HPDF_Page_Rectangle(page1,?200,?200,?40,?150);
????HPDF_Page_Stroke(page1);
????LOG(@"[libharu]?PNG?image?drawing?page?1");
????//?comment?out?this?line?intentionally?causes?an?error?here?to?test?error?handling
//????path?=?[[NSBundle?mainBundle]?pathForResource:@"no_such_file_hogehoge"
//???????????????????????????????????????????ofType:@"png"];
????path?=?[[NSBundle?mainBundle]?pathForResource:@"test"
???????????????????????????????????????????ofType:@"png"];
????pathCString?=?[path?cStringUsingEncoding:NSASCIIStringEncoding];
????LOG(@"[libharu]?LoadPngImageFromFile?path:%@\n?pathCString:%s",?path,?pathCString);
????HPDF_Image?image?=?HPDF_LoadPngImageFromFile(pdf,?pathCString);
????HPDF_Page_DrawImage(page1,?image,?260,?240,?245,?319);
????pathCString?=?[filePath?cStringUsingEncoding:1];
????LOG(@"[libharu]?SaveToFile?filePath:%@\n?pathCString:%s",?filePath,?pathCString);
????HPDF_SaveToFile(pdf,?pathCString);
????LOG(@"[libharu]?Freeing?PDF?object?");
????if?(HPDF_HasDoc(pdf))?{
????????HPDF_Free(pdf);
????}
????LOG(@"[libharu]?PDF?Creation?END");
}
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-152376-1.html
你是厂家派来的逗比吗
全世界能单挑赢的船应该不超过10艘