LibHaru编译使用

最近公司准备在PDF方面发力了,我也要行动起来,就找到了LibHaru这个开源库

 

编译Libharu需要用到zlib库和libpng库,libpng库又依赖zlib库.

zlib 下载地址:http://www.zlib.net/

libpng下载地址:http://www.libpng.org/pub/png/libpng.html

libharu下载地址:http://libharu.org/

 

下载好后,最好放到一个单独的文件夹下,

 进入目录  D:\Users\PDF\lpng\projects\visualc71 里面有VS的工程,根据自己的需求编译DLL或者LIb选择不同的编译选项,

 

设置libpng工程的zlib头文件路径,zlib工程和上图一样,编译后,生成libpng.lib和zlib.lib,2个静态库。

为了方便编译Libharu,将zlib和libpng的头文件与静态库,单独存放到各自的include和lib文件夹里。

 

修改D:\Users\PDF\libharu\script\Makefile.msvc文件,如下图所示的2处

管理员 运行 “VS2008命令提示” ,

cd /D D:\Users\PDF\libharu

nmake script/Makefile.msvc

 

至此,LibHaru编译完成。

创建win32命令行工程测试下。

  1 #include <stdlib.h>
  2 #include <stdio.h>
  3 #include <string.h>
  4 #include <setjmp.h>
  5 #include "hpdf.h"
  6 
  7 #ifndef HPDF_NOPNGLIB
  8 
  9 jmp_buf env;
 10 
 11 #ifdef HPDF_DLL
 12 void  __stdcall
 13 #else
 14 void
 15 #endif
 16 error_handler  (HPDF_STATUS   error_no,
 17                 HPDF_STATUS   detail_no,
 18                 void         *user_data)
 19 {
 20     printf ("ERROR: error_no=%04X, detail_no=%u\n", (HPDF_UINT)error_no,
 21                 (HPDF_UINT)detail_no);
 22     longjmp(env, 1);
 23 }
 24 
 25 void
 26 draw_image (HPDF_Doc     pdf,
 27             const char  *filename,
 28             float        x,
 29             float        y,
 30             const char  *text)
 31 {
 32 #ifdef __WIN32__
 33     const char* FILE_SEPARATOR = "\\";
 34 #else
 35     const char* FILE_SEPARATOR = "/";
 36 #endif
 37     char filename1[255];
 38 
 39     HPDF_Page page = HPDF_GetCurrentPage (pdf);
 40     HPDF_Image image;
 41 
 42     strcpy(filename1, "pngsuite");
 43     strcat(filename1, FILE_SEPARATOR);
 44     strcat(filename1, filename);
 45 
 46     image = HPDF_LoadPngImageFromFile (pdf, filename1);
 47 
 48     /* Draw image to the canvas. */
 49     HPDF_Page_DrawImage (page, image, x, y, HPDF_Image_GetWidth (image),
 50                     HPDF_Image_GetHeight (image));
 51 
 52     /* Print the text. */
 53     HPDF_Page_BeginText (page);
 54     HPDF_Page_SetTextLeading (page, 16);
 55     HPDF_Page_MoveTextPos (page, x, y);
 56     HPDF_Page_ShowTextNextLine (page, filename);
 57     HPDF_Page_ShowTextNextLine (page, text);
 58     HPDF_Page_EndText (page);
 59 }
 60 
 61 
 62 int main (int argc, char **argv)
 63 {
 64     HPDF_Doc  pdf;
 65     HPDF_Font font;
 66     HPDF_Page page;
 67     char fname[256];
 68     HPDF_Destination dst;
 69 
 70     strcpy (fname, argv[1]);
 71     strcat (fname, ".pdf");
 72 
 73     pdf = HPDF_New (error_handler, NULL);
 74     if (!pdf) {
 75         printf ("error: cannot create PdfDoc object\n");
 76         return 1;
 77     }
 78 
 79     /* error-handler */
 80     if (setjmp(env)) {
 81         HPDF_Free (pdf);
 82         return 1;
 83     }
 84 
 85     HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL);
 86 
 87     /* create default-font */
 88     font = HPDF_GetFont (pdf, "Helvetica", NULL);
 89 
 90     /* add a new page object. */
 91     page = HPDF_AddPage (pdf);
 92 
 93     HPDF_Page_SetWidth (page, 550);
 94     HPDF_Page_SetHeight (page, 650);
 95 
 96     dst = HPDF_Page_CreateDestination (page);
 97     HPDF_Destination_SetXYZ (dst, 0, HPDF_Page_GetHeight (page), 1);
 98     HPDF_SetOpenAction(pdf, dst);
 99 
100     HPDF_Page_BeginText (page);
101     HPDF_Page_SetFontAndSize (page, font, 20);
102     HPDF_Page_MoveTextPos (page, 220, HPDF_Page_GetHeight (page) - 70);
103     HPDF_Page_ShowText (page, "PngDemo");
104     HPDF_Page_EndText (page);
105 
106     HPDF_Page_SetFontAndSize (page, font, 12);
107 
108     draw_image (pdf, "hand.png", 50, 50/*HPDF_Page_GetHeight (page)*/,
109                 "1bit grayscale.");
110  
111 
112     /* save the document to a file */
113     HPDF_SaveToFile (pdf, fname);
114 
115     /* clean up */
116     HPDF_Free (pdf);
117 
118     return 0;
119 }
120 
121 #else /* HPDF_NOPNGLIB */
122 
123 int main()
124 {
125     printf("WARNING: if you want to run this demo, \n"
126            "make libhpdf with HPDF_USE_PNGLIB option.\n");
127     return 0;
128 }
129 
130 #endif /* HPDF_NOPNGLIB */

工程头文件目录

D:\Users\PDF\libharu\include;

D:\Users\PDF\lpng\include;

D:\Users\PDF\zlib\include;

D:\Users\PDF\libharu\win32\include

工程库文件目录

D:\Users\PDF\libharu;

D:\Users\PDF\lpng\lib

工程使用库

libhpdf.lib libpng.lib zlib.lib

posted @ 2018-04-25 21:30  dozeoo  阅读(5291)  评论(2编辑  收藏  举报