1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
#include <stdio.h>
#include <stdlib.h>
#include <mysql/mysql.h>
#include "hpdf.h"
void error_handler(HPDF_STATUS error_no,
HPDF_STATUS detail_no,
void *user_data)
{
printf ("ERROR: error_no=%04X, detail_no=%u\n",
(HPDF_UINT)error_no, (HPDF_UINT)detail_no);
}
int main() {
MYSQL *con = mysql_init(NULL);
if (con == NULL) {
fprintf(stderr, "%s\n", mysql_error(con));
exit(1);
}
if (mysql_real_connect(con, "localhost", "root", "root_password",
"test_db", 0, NULL, 0) == NULL) {
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
if (mysql_query(con, "SELECT * FROM table_name")) {
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
MYSQL_RES *result = mysql_store_result(con);
if (result == NULL) {
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
// Create a PDF
HPDF_Doc pdf = HPDF_New(error_handler, NULL);
HPDF_Page page = HPDF_AddPage(pdf);
HPDF_Page_SetSize(page, HPDF_PAGE_SIZE_A4, HPDF_PAGE_PORTRAIT);
HPDF_Font font = HPDF_GetFont(pdf, "Helvetica", NULL);
HPDF_Page_SetFontAndSize(page, font, 24);
int row_count = 0;
MYSQL_ROW row;
while ((row = mysql_fetch_row(result))) {
HPDF_Page_BeginText(page);
HPDF_Page_TextOut(page, 50, 750 - row_count * 30, row[0]); // Assumer que row[0] est une chaîne
HPDF_Page_EndText(page);
row_count++;
}
HPDF_SaveToFile(pdf, "output.pdf");
HPDF_Free(pdf);
mysql_free_result(result);
mysql_close(con);
exit(0);
} |
Partager