Invoice++
Loading...
Searching...
No Matches
pdf_utils.h
Go to the documentation of this file.
1#ifndef PDF_UTILS_H
2#define PDF_UTILS_H
3
4#include <hpdf.h>
5#include <iostream>
6#include <string>
7
8#include "pdf_exception.h"
9#include "../utils.h"
10
13inline void
14 error_handler (const HPDF_STATUS error_no,
15 const HPDF_STATUS detail_no,
16 void *user_data)
17{
18 throw PDFException(error_no, detail_no);
19}
20
23inline HPDF_REAL calculate_text_height(const HPDF_Page page, const std::string &text, const HPDF_REAL max_width,
24 const HPDF_REAL line_height, const HPDF_REAL max_lines = NULL) {
25 HPDF_REAL real_width;
26 std::string current_text = text;
27 size_t total_lines = 0;
28
29 while (!current_text.empty()) {
30 const HPDF_UINT len = HPDF_Page_MeasureText(page, current_text.c_str(), max_width, HPDF_TRUE, &real_width);
31 if (len == 0) {
32 break;
33 }
34 total_lines++;
35 current_text = current_text.substr(len);
36 if (max_lines != NULL and total_lines == max_lines) {
37 break;
38 }
39 }
40
41 return total_lines * line_height;
42}
43
46inline HPDF_REAL writeText(const HPDF_Page page, std::string text, const HPDF_REAL left,
47 const HPDF_REAL top, const HPDF_REAL line_height, const HPDF_REAL max_width, const _HPDF_TextAlignment alignment, const HPDF_REAL max_height) {
48
49 const HPDF_REAL bottom = (max_height == NULL) ? NULL : (top - max_height);
50
51 text = UTF8toISO8859_1(text);
52
53 const HPDF_REAL textHeight = calculate_text_height(page, text, max_width, line_height, max_height);
54
55 HPDF_Page_BeginText(page);
56 HPDF_Page_TextRect(page, left, top, left + max_width, bottom, text.c_str(), alignment,
57 nullptr);
58 HPDF_Page_EndText(page);
59 return textHeight;
60}
61
64inline HPDF_REAL writeTextIfNotHeightExceeded(const HPDF_Page page, std::string text, const HPDF_REAL left,
65 const HPDF_REAL top, const HPDF_REAL line_height, const HPDF_REAL max_width, const _HPDF_TextAlignment alignment, const HPDF_REAL max_height) {
66
67 const HPDF_REAL bottom = (max_height == NULL) ? NULL : (top - max_height);
68
69 text = UTF8toISO8859_1(text);
70
71 const HPDF_REAL textHeight = calculate_text_height(page, text, max_width, line_height, NULL);
72
73 if (textHeight > max_height) {
74 return 0;
75 }
76
77 HPDF_Page_BeginText(page);
78 HPDF_Page_TextRect(page, left, top, left + max_width, bottom, text.c_str(), alignment,
79 nullptr);
80 HPDF_Page_EndText(page);
81 return textHeight;
82}
83
86inline HPDF_REAL writeText(const HPDF_Page page, std::string text, const HPDF_REAL left,
87 const HPDF_REAL top, const HPDF_REAL line_height, const HPDF_REAL max_width, const _HPDF_TextAlignment alignment, const int max_lines = 0) {
88
89 const HPDF_REAL bottom = (max_lines == NULL) ? NULL : (top - max_lines * line_height);
90
91 text = UTF8toISO8859_1(text);
92
93 const HPDF_REAL textHeight = calculate_text_height(page, text, max_width, line_height, max_lines);
94
95 HPDF_Page_BeginText(page);
96 HPDF_Page_TextRect(page, left, top, left + max_width, bottom, text.c_str(), alignment,
97 nullptr);
98 HPDF_Page_EndText(page);
99 return textHeight;
100}
101
104inline HPDF_REAL writeTextIfNotLinesExceeded(const HPDF_Page page, std::string text, const HPDF_REAL left,
105 const HPDF_REAL top, const HPDF_REAL line_height, const HPDF_REAL max_width, const _HPDF_TextAlignment alignment, const int max_lines = 0) {
106
107 const HPDF_REAL bottom = (max_lines == NULL) ? NULL : (top - max_lines * line_height);
108
109 text = UTF8toISO8859_1(text);
110
111 const HPDF_REAL textHeight = calculate_text_height(page, text, max_width, line_height, NULL);
112 std::cout << textHeight << " " << max_lines * line_height << std::endl;
113 if (textHeight > max_lines * line_height) {
114 return 0;
115 }
116 HPDF_Page_BeginText(page);
117 HPDF_Page_TextRect(page, left, top, left + max_width, bottom, text.c_str(), alignment,
118 nullptr);
119 HPDF_Page_EndText(page);
120 return textHeight;
121}
122
125inline HPDF_REAL changeFont(const HPDF_Page page, const HPDF_REAL font_size, const HPDF_Font font) {
126 const float line_height = font_size * 1.2;
127 HPDF_Page_SetFontAndSize(page, font, font_size);
128 HPDF_Page_SetTextLeading(page, line_height);
129 return line_height;
130}
131
133inline void drawLine(const HPDF_Page page, const HPDF_REAL from_x, const HPDF_REAL from_y, const HPDF_REAL to_x, const HPDF_REAL to_y) {
134 HPDF_Page_MoveTo(page, from_x, from_y);
135 HPDF_Page_LineTo(page, to_x, to_y);
136 HPDF_Page_Stroke(page);
137}
138
139#endif //PDF_UTILS_H
Libharu Exception.
Definition pdf_exception.h:7
void error_handler(const HPDF_STATUS error_no, const HPDF_STATUS detail_no, void *user_data)
libharu error handler
Definition pdf_utils.h:14
HPDF_REAL calculate_text_height(const HPDF_Page page, const std::string &text, const HPDF_REAL max_width, const HPDF_REAL line_height, const HPDF_REAL max_lines=NULL)
calculates the height of a given text on the pdf
Definition pdf_utils.h:23
HPDF_REAL writeTextIfNotLinesExceeded(const HPDF_Page page, std::string text, const HPDF_REAL left, const HPDF_REAL top, const HPDF_REAL line_height, const HPDF_REAL max_width, const _HPDF_TextAlignment alignment, const int max_lines=0)
write text on to the pdf at a given point when max lines is not exceeded
Definition pdf_utils.h:104
HPDF_REAL writeText(const HPDF_Page page, std::string text, const HPDF_REAL left, const HPDF_REAL top, const HPDF_REAL line_height, const HPDF_REAL max_width, const _HPDF_TextAlignment alignment, const HPDF_REAL max_height)
write text on to the pdf at a given point
Definition pdf_utils.h:46
HPDF_REAL writeTextIfNotHeightExceeded(const HPDF_Page page, std::string text, const HPDF_REAL left, const HPDF_REAL top, const HPDF_REAL line_height, const HPDF_REAL max_width, const _HPDF_TextAlignment alignment, const HPDF_REAL max_height)
write text on to the pdf at a given point when max height is not exceeded
Definition pdf_utils.h:64
HPDF_REAL changeFont(const HPDF_Page page, const HPDF_REAL font_size, const HPDF_Font font)
change the font and font size
Definition pdf_utils.h:125
void drawLine(const HPDF_Page page, const HPDF_REAL from_x, const HPDF_REAL from_y, const HPDF_REAL to_x, const HPDF_REAL to_y)
draw a line, line width needs to be set before
Definition pdf_utils.h:133
std::string UTF8toISO8859_1(const std::string &utf8_str)
converts a UTF8 string to ISO8859-1 / ANSI using iconv
Definition utils.h:11