You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
5.7 KiB

  1. /*
  2. * << Haru Free PDF Library 2.0.0 >> -- font_demo.cpp
  3. *
  4. * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
  5. *
  6. * Permission to use, copy, modify, distribute and sell this software
  7. * and its documentation for any purpose is hereby granted without fee,
  8. * provided that the above copyright notice appear in all copies and
  9. * that both that copyright notice and this permission notice appear
  10. * in supporting documentation.
  11. * It is provided "as is" without express or implied warranty.
  12. *
  13. */
  14. #include <iconv.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <string>
  19. // #include <exception>
  20. #include "hpdf.h"
  21. #ifdef HPDF_DLL
  22. void __stdcall
  23. #else
  24. void
  25. #endif
  26. error_handler(HPDF_STATUS error_no, HPDF_STATUS detail_no, void* user_data) {
  27. printf("ERROR: error_no=%04X, detail_no=%u\n", (HPDF_UINT)error_no, (HPDF_UINT)detail_no);
  28. }
  29. // const char *font_list[] = {"SimHei", NULL};
  30. // int main(int argc, char **argv) {
  31. // const char *page_title = "Font Demo";
  32. // HPDF_Doc pdf;
  33. // char fname[256];
  34. // HPDF_Page page;
  35. // HPDF_Font def_font;
  36. // HPDF_REAL tw;
  37. // HPDF_REAL height;
  38. // HPDF_REAL width;
  39. // HPDF_UINT i;
  40. // strcpy(fname, argv[0]);
  41. // strcat(fname, ".pdf");
  42. // pdf = HPDF_New(error_handler, NULL);
  43. // if (!pdf) {
  44. // printf("error: cannot create PdfDoc object\n");
  45. // return 1;
  46. // }
  47. // HPDF_STATUS statu = HPDF_UseCNSFonts(pdf);
  48. // printf("HPDF_UseCNSFonts: %x\n", statu);
  49. // statu = HPDF_UseCNTFonts(pdf);
  50. // printf("HPDF_UseCNTFonts: %x\n", statu);
  51. // statu = HPDF_UseCNTEncodings(pdf);
  52. // printf("HPDF_UseCNTEncodings: %x\n", statu);
  53. // statu = HPDF_UseCNSEncodings(pdf);
  54. // printf("HPDF_UseCNSEncodings: %x\n", statu);
  55. // try {
  56. // /* Add a new page object. */
  57. // page = HPDF_AddPage(pdf);
  58. // height = HPDF_Page_GetHeight(page);
  59. // width = HPDF_Page_GetWidth(page);
  60. // /* Print the lines of the page. */
  61. // HPDF_Page_SetLineWidth(page, 1);
  62. // HPDF_Page_Rectangle(page, 50, 50, width - 100, height - 110);
  63. // HPDF_Page_Stroke(page);
  64. // /* Print the title of the page (with positioning center). */
  65. // def_font = HPDF_GetFont(pdf, "Helvetica", NULL);
  66. // HPDF_Page_SetFontAndSize(page, def_font, 24);
  67. // tw = HPDF_Page_TextWidth(page, page_title);
  68. // HPDF_Page_BeginText(page);
  69. // HPDF_Page_MoveTextPos(page, (width - tw) / 2, height - 50);
  70. // HPDF_Page_ShowText(page, page_title);
  71. // HPDF_Page_EndText(page);
  72. // /* output subtitle. */
  73. // HPDF_Page_BeginText(page);
  74. // HPDF_Page_MoveTextPos(page, 60, height - 80);
  75. // HPDF_Page_SetFontAndSize(page, def_font, 16);
  76. // HPDF_Page_ShowText(page, "<Standerd Type1 fonts samples>");
  77. // HPDF_Page_EndText(page);
  78. // HPDF_Page_BeginText(page);
  79. // HPDF_Page_MoveTextPos(page, 60, height - 105);
  80. // i = 0;
  81. // while (font_list[i]) {
  82. // const char *samp_text = "abcd";
  83. // HPDF_Font font = HPDF_GetFont(pdf, font_list[i], NULL);
  84. // if (!font) {
  85. // printf("error: cannot get %s font\n", font_list[i]);
  86. // return 1;
  87. // }
  88. // /* print a label of text */
  89. // HPDF_Page_SetFontAndSize(page, def_font, 9);
  90. // HPDF_Page_ShowText(page, font_list[i]);
  91. // HPDF_Page_MoveTextPos(page, 0, -18);
  92. // /* print a sample text. */
  93. // HPDF_Page_SetFontAndSize(page, font, 20);
  94. // HPDF_Page_ShowText(page, samp_text);
  95. // HPDF_Page_MoveTextPos(page, 0, -20);
  96. // i++;
  97. // }
  98. // HPDF_Page_EndText(page);
  99. // HPDF_SaveToFile(pdf, fname);
  100. // } catch (...) {
  101. // HPDF_Free(pdf);
  102. // return 1;
  103. // }
  104. // /* clean up */
  105. // HPDF_Free(pdf);
  106. // return 0;
  107. // }
  108. using namespace std;
  109. string utf8_to_gb2312(const std::string& utf8_str) {
  110. // 打开转换描述符
  111. iconv_t cd = iconv_open("GB2312", "UTF-8");
  112. if (cd == (iconv_t)-1) {
  113. perror("iconv_open failed");
  114. exit(EXIT_FAILURE);
  115. }
  116. // 输入字符串
  117. const char* in_str = utf8_str.c_str();
  118. size_t in_size = utf8_str.size();
  119. // 输出缓冲区
  120. size_t out_size = in_size * 2; // 假设输出的字节数不会超过输入的两倍
  121. char* out_buf = new char[out_size];
  122. char* out_str = out_buf;
  123. // 进行转换
  124. if (iconv(cd, const_cast<char**>(&in_str), &in_size, &out_str, &out_size) == (size_t)-1) {
  125. perror("iconv failed");
  126. iconv_close(cd);
  127. delete[] out_buf;
  128. exit(EXIT_FAILURE);
  129. }
  130. // 关闭转换描述符
  131. iconv_close(cd);
  132. // 获取转换后的字符串
  133. std::string gb2312_str(out_buf, out_str - out_buf);
  134. // 释放内存
  135. delete[] out_buf;
  136. return gb2312_str;
  137. }
  138. int main() {
  139. HPDF_Doc pdf;
  140. HPDF_Font font;
  141. HPDF_Page szPage[10115];
  142. HPDF_REAL tw;
  143. string strPdfContent = utf8_to_gb2312("这是一个测试,支持中文");
  144. string strPdfName = "XXX.pdf";
  145. pdf = HPDF_New(error_handler, NULL);
  146. HPDF_UseCNSFonts(pdf);
  147. HPDF_UseCNTFonts(pdf);
  148. HPDF_UseCNTEncodings(pdf);
  149. HPDF_UseCNSEncodings(pdf);
  150. // support Chinese Song (SimSun)and Chinese black (SimHei)
  151. font = HPDF_GetFont(pdf, "SimSun", "GBK-EUC-H");
  152. if (!font) {
  153. printf("error: cannot get font\n");
  154. return 1;
  155. }
  156. for (int i = 0; i < 10115; ++i) {
  157. szPage[i] = HPDF_AddPage(pdf);
  158. HPDF_Page_SetSize(szPage[i], HPDF_PAGE_SIZE_LETTER, HPDF_PAGE_PORTRAIT);
  159. HPDF_Page_BeginText(szPage[i]);
  160. HPDF_Page_SetFontAndSize(szPage[i], font, 20);
  161. tw = HPDF_Page_TextWidth(szPage[i], strPdfContent.c_str());
  162. HPDF_Page_MoveTextPos(szPage[i], (HPDF_Page_GetWidth(szPage[i]) - tw) / 2, (HPDF_Page_GetHeight(szPage[i]) - 20) / 2);
  163. HPDF_Page_ShowText(szPage[i], strPdfContent.c_str());
  164. HPDF_Page_EndText(szPage[i]);
  165. }
  166. HPDF_SaveToFile(pdf, strPdfName.c_str());
  167. HPDF_Free(pdf);
  168. }