Génération de PDF


26 août 2007

Applications -> Framework

#include <libxml/parser.h>
#include "pdflib.h"
    //Essai d'utilisation de LibXML2
    xmlDocPtr doc;
    xmlNodePtr cur;
    doc = xmlParseFile("synthereports.xml");
    //doc = xmlParseFile("test.xml");
    xmlErrorPtr LastErr = xmlGetLastError();
    if (doc == NULL ) {
        fprintf(stderr, "Document not parsed successfully. \n");
        return EXIT_FAILURE;
    }
    cur = xmlDocGetRootElement(doc);
    if (cur == NULL) {
        fprintf(stderr,"empty document\n");
        xmlFreeDoc(doc);
        return EXIT_FAILURE;
    }
    if (xmlStrcmp(cur->name, (const xmlChar *) "html")) {
        fprintf(stderr,"document of the wrong type, root node != html");
        xmlFreeDoc(doc);
        return EXIT_FAILURE;
    }
    //Essai d'utilisation de PDFLib
    PDF *p;
    int font;
    // create a new PDFlib object
    if ((p = PDF_new()) == (PDF *) 0)
    {
        printf("Couldn't create PDFlib object (out of memory)!\n");
        return(2);
    }

    PDF_TRY(p) {
        /* This means we must check return values of load_font() etc. */
        PDF_set_parameter(p, "errorpolicy", "return");

        if (PDF_begin_document(p, "hello.pdf", 0, "") == -1) {
            printf("Error: %s\n", PDF_get_errmsg(p));
            return(2);
        }

        /* This line is required to avoid problems on Japanese systems */
        PDF_set_parameter(p, "hypertextencoding", "host");

        PDF_set_info(p, "Creator", "hello.c");
        PDF_set_info(p, "Author", "Thomas Merz");
        PDF_set_info(p, "Title", "Hello, world (C)!");

        PDF_begin_page_ext(p, a4_width, a4_height, "");

        /* Change "host" encoding to "winansi" or whatever you need! */
        font = PDF_load_font(p, "Helvetica-Bold", 0, "host", "");
        if (font == -1) {
            printf("Error: %s\n", PDF_get_errmsg(p));
            PDF_delete(p);
            return(2);
        }
        PDF_setfont(p, font, 24);
        PDF_set_text_pos(p, 50, 700);
        PDF_show(p, "Hello, world!");
        PDF_continue_text(p, "(says C)");
        //
        int font2 = PDF_load_font(p, "Times", 0, "host", "");
        if (font2 == -1) {
            printf("Error: %s\n", PDF_get_errmsg(p));
            PDF_delete(p);
            return(2);
        }
        PDF_setfont(p, font2, 24);
        PDF_set_text_pos(p, 50, 500);
        PDF_show(p, "Suite...");
        PDF_continue_text(p, "(says Flubb)");
        //
        PDF_end_page_ext(p, "");

        PDF_end_document(p, "");
    }

    PDF_CATCH(p) {
        printf("PDFlib exception occurred in hello sample:\n");
        printf("[%d] %s: %s\n",
                PDF_get_errnum(p), PDF_get_apiname(p), PDF_get_errmsg(p));
        PDF_delete(p);
        return(2);
    }

   PDF_delete(p);
Accueil