How can I view / open / render / display a pdf file with c code?

Joined
Sep 12, 2023
Messages
1
Reaction score
0
I would like to create a gtk app that creates and displays a pdf file (but for the sake of this question: if you know how to do it without gtk, I'd love to know!). This file has been created with the same gtk app. I'm new to C and only know basics (mostly js, bash, LO basic). Besides looking for a very long time I couldn't find anything about how to display a pdf file – not even if we ignore gtk.

Apparently poppler and ghostscript can be used for this purpose, but I found not a single example file, tutorial or video that actually describes how to display a pdf.


Here's my current code (main.c):

#include <stdio.h>
#include <hpdf.h>
#include <setjmp.h> //for jmp_buf
#include <gtk/gtk.h>

/*
* Includes libharu from http://libharu.org/ for building pdf.
*/

jmp_buf g_env;


void error_handler(HPDF_STATUS error_no, HPDF_STATUS detail_no, void *user_data);
static int create_pdf();
static void activate(GtkApplication* app, gpointer user_data);


/**
* @brief Main function. Creates the GTK Application. The contents of the window will be created in
* seperate functions. Also it creates a pdf function and saves it (as a test. Main should only
* create the application).
* @param argc
* @param argv
* @return
*/
int main (int argc, char **argv)
{
GtkApplication *app;
int status;

app = gtk_application_new("org.test", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);

// status is the exit code when the app gets closed
status = g_application_run(G_APPLICATION(app), argc, argv);
// free app from memory
g_object_unref(app);


create_pdf();
printf("DONE\n");


return status;
}


/**
* @brief Create a pdf file with the use of hpdf and save it to downloads folder.
* @return
*/
static int create_pdf()
{
const char *PAGE_TITLE = "Document title";
HPDF_Doc pdf;
HPDF_Page page_1;
HPDF_Font def_font;
HPDF_REAL text_width;


pdf = HPDF_New (error_handler, NULL);
if (!pdf) {
printf ("ERROR: cannot create pdf object.\n");
return 1;
}

if (setjmp(g_env)) {
HPDF_Free (pdf);
return 1;
}

//set document object attributes such as compression and password
HPDF_SetCompressionMode(pdf, HPDF_COMP_ALL);

//create a new page
page_1 = HPDF_AddPage(pdf);

//set page object attributes
HPDF_Page_SetSize(page_1, HPDF_PAGE_SIZE_A4, HPDF_PAGE_LAYOUT_SINGLE);

//page description, which includes content
def_font = HPDF_GetFont(pdf, "Courier", NULL);
HPDF_Page_SetFontAndSize(page_1, def_font, 42);

// for title
text_width = HPDF_Page_TextWidth(page_1, PAGE_TITLE);
HPDF_Page_BeginText(page_1);
HPDF_Page_TextOut(page_1, (HPDF_Page_GetWidth(page_1) - text_width) / 2, HPDF_Page_GetHeight(page_1) - 50, PAGE_TITLE);
HPDF_Page_EndText(page_1);

//for some text
HPDF_Page_SetFontAndSize(page_1, def_font, 12);

HPDF_Page_BeginText(page_1);
HPDF_Page_MoveTextPos(page_1, 60, HPDF_Page_GetHeight(page_1) - 105);
HPDF_Page_ShowText(page_1, "Hello World");
HPDF_Page_EndText(page_1);


//save document to file
HPDF_SaveToFile(pdf, "Downloads/test_this_file_has_been_created_by_org.test.pdf");

//free resources
HPDF_Free(pdf);

return 0;
}


/**
* @brief On error, this function displays the error code. See
* https://github.com/libharu/libharu/wiki/Error-handling for code meaning.
* @param error_no
* @param detail_no
* @param user_data
*/
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);
longjmp(g_env, 1); /* invoke longjmp() on error */
}


/**
* @brief Used to construct the window so that the window is shown when the application is launched.
* It sets window data such as the window title and size.
* @param app
* @param user_data
*/
static void activate(GtkApplication* app, gpointer user_data)
{
GtkWidget *window;

window = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW(window), "TEST");
gtk_window_set_default_size(GTK_WINDOW(window), 200, 200);
gtk_widget_show_all(window);
}

(You could make the code much shorter by ignoring everything inside of main() and only calling create_pdf()).

The code executes with 0 errors and 0 warnings (using gcc) on my machine.


Also: I'm looking for C, not C++, C#, .NET or any other language that is not C.

I am coding with Codelite 14.0.0 on Debian 11, bullseye, with GNOME 3.38.5 as my desktop environment.


Thank you in advance for all of your help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top