michaelrsweet / pdfio

PDFio is a simple C library for reading and writing PDF files.
https://www.msweet.org/pdfio
Apache License 2.0
199 stars 44 forks source link

Would you be so kind to write a plain simple example of pdf writing? #69

Closed raphael10-collab closed 3 months ago

raphael10-collab commented 4 months ago

Would you be so kind to write a plain simple example of pdf writing?

For example:

    std::string PdfFileName = "output.pdf";
    std::string completePathToPdfFile = PdfCreatedPath + "/" + PdfFileName;

    pdfio_rect_t media_box = { 0.0, 0.0, 612.0, 792.0 };  // US Letter
    pdfio_rect_t crop_box = { 36.0, 36.0, 576.0, 756.0 }; // w/0.5" margins
    pdfio_output_cb_t output_cb;
    void *output_cbdata;
    pdfio_error_cb_t error_cb;
    void  *error_cb_data;
    pdfio_file_t *pdf = pdfioFileCreateOutput(output_cb, output_cbdata, "2.0", &media_box, &crop_box, error_cb, error_cb_data);

How to set the file path ?

michaelrsweet commented 4 months ago

I'll investigate providing some more complete examples using the content APIs.

In the meantime, if you want to write to a named file, use the pdfioFileCreate function - pdfioFileCreateOutput is intended for streaming PDF output to some destination - it might be a file, or a socket, or an in-memory buffer.

raphael10-collab commented 4 months ago

Thank you Michael

michaelrsweet commented 4 months ago

Using this issue to track documentation improvements. I'll close when done.

raphael10-collab commented 4 months ago

Ok. Thank you @michaelrsweet

In the meanwhile, I kindly ask you to have a look at this code:

    pdfio_rect_t media_box = { 0.0, 0.0, 612.0, 792.0 };  // US Letter
    pdfio_rect_t crop_box = { 36.0, 36.0, 576.0, 756.0 }; // w/0.5" margins
    pdfio_output_cb_t output_cb;
    void *output_cbdata;
    pdfio_error_cb_t error_cb;

    void  *error_cb_data;
    int outfd;
    bool error = false;
    // https://github.com/michaelrsweet/pdfio/blob/master/pdfio-file.c
    // testpdfio.c
    pdfio_file_t *pdf = pdfioFileCreate(completePathToPdfFile.c_str(), NULL, NULL, NULL, (pdfio_error_cb_t)error_cb, &error);
    pdfio_file_t *inputpdf = pdfioFileOpen("/home/raphy/GraspAppData/PdfCreated/output.pdf", NULL, NULL, (pdfio_error_cb_t)error_cb, &error);
    pdfio_obj_t *courier = pdfioFileCreateFontObjFromBase(pdf, "Courier");
    pdfio_dict_t  *dict = pdfioDictCreate(pdf);
    pdfioPageDictAddFont(dict, "F2", courier);

    pdfio_stream_t *st = pdfioFileCreatePage(pdf, dict);

    static int num_pages = pdfioFileGetNumPages(pdf);
    std::cout << " static int num_pages = pdfioFileGetNumPages(pdf) : " << num_pages << std::endl;

    //pdfioContentSetFillColorDeviceGray(st, 0.0);
    //pdfioContentTextBegin(st);
    //pdfioContentSetTextFont(st, "F1", 18.0);
    //pdfioContentTextMoveTo(st, 36.0, 738.0);
    //char title[256] = "My First Pdf";
    //pdfioContentTextShow(st, true, title);
    //pdfioContentSetTextFont(st, "F1", 12.0);
    //pdfioContentTextMoveTo(st, 514.0, -702.0);

    pdfioDictSetName(dict, "Type", "XObject");
    pdfioDictSetName(dict, "Subtype", "Image");
    pdfioDictSetNumber(dict, "Width", 256);
    pdfioDictSetNumber(dict, "Height", 256);
    pdfioDictSetNumber(dict, "BitsPerComponent", 8);
    pdfioDictSetName(dict, "ColorSpace", "DeviceRGB");
    pdfioDictSetName(dict, "Filter", "FlateDecode");
    pdfio_obj_t *obj = pdfioFileCreateObj(pdf, dict);

    // https://www.msweet.org/pdfio/pdfio.html#image-object-functions
    unsigned char data[1024 * 1024 * 4]; // 1024x1024 RGBA image data
    pdfio_obj_t *img = pdfioFileCreateImageObjFromFile(pdf, "/home/raphy/Pictures/camera.jpg", /*interpolate*/true);

     if (!pdfioPageDictAddImage(dict,  "pdfio-color", img))
    {
        std::cout << "Failing in adding pdfio-color.png" << std::endl;
    }

    if (!pdfioContentTextBegin(st))
    {
        std::cout << "Failing in beginning text" << std::endl;
    }

    if (!pdfioContentSetTextLeading(st, 30))
    {
        std::cout << "Failing in Setting Line Height" << std::endl;
    }

    if (!pdfioContentTextNewLineShow(st, 1.0, 0.0, true, "MY FIRST TEXT"))
    {
        std::cout << "Failing in show text line" << std::endl;
    }

    if (!pdfioContentTextEnd(st))
    {
        std::cout << "Failing in ending text" << std::endl;
    }

    if (!pdfioContentSetStrokeColorRGB(st,0, 0, 153))
    {
        std::cout << "Failing ColorRGB Stroke" << std::endl;
    }

    pdfioFileClose(pdf);

Output:

Failing in adding pdfio-color.png

and a pdf file with one empty page, with no line of text, no colored stroke and image.

image

image

And I'm getting "img null" :

    pdfio_obj_t *img = pdfioFileCreateImageObjFromFile(pdf, "/home/raphy/Pictures/camera.jpg", /*interpolate*/true);
    if (img != NULL)
    {
        std::cout << "img not null" << std::endl;
    }
    else
    {
        std::cout << "img null" << std::endl; 
    }

How to correctly add text and add an image file to the empty page of the created PDF file ?

devnibo commented 4 months ago

Hello @raphael10-collab if you're interested I could share a personal project of mine with you in which I use the pdfio library in my C project. I also miss example code how to use the library but some things I did figure out by now.

devnibo commented 4 months ago

I'll investigate providing some more complete examples using the content APIs.

@michaelrsweet if you already have concrete plans of what examples you want to provide, maybe I can write one or two of them. Just so you know I would willing to do so.

raphael10-collab commented 4 months ago

@devnibo I actually didn't manage to even create a simple plain empty pdf page, as you can see here : https://github.com/michaelrsweet/pdfio/issues/69#issuecomment-2225556190

So, any working project examples are welcome

I thank you very much

michaelrsweet commented 3 months ago

[master 1d4f77c] Add examples to documentation (Issue #69)