skip to Main Content

I started working with PoDoFo a few weeks ago. Previously, I have worked with cairomm. I am creating a PDF output with PoDoFo in Visual Studio 2022 but when trying to insert a PNG image a have compilation error regarding PdfImage constructor.

I am trying the following code:

#include <iostream>
#include <podofo/podofo.h>

using namespace std;
using namespace PoDoFo;

void PrintLogo(const string_view& filename) {
    PdfMemDocument document;
    PdfPainter painter;
    PdfFont* font;

    string imageFile = "C:\image.png";

    try {
        auto& page = document.GetPages().CreatePage(PdfPage::CreateStandardPageSize(PdfPageSize::A4));
        painter.SetCanvas(page);

        font = document.GetFonts().SearchFont("Arial");
        if (!font) throw runtime_error("Invalid handle");

        painter.TextState.SetFont(*font, 18);
        painter.GraphicsState.SetFillColor(PdfColor(1, 0.5, 0.5));
        painter.GraphicsState.SetStrokeColor(PdfColor(1, 0.0, 0.0));

        PdfImage image(&document);
        //image.Load(imageFile);
        //painter.DrawImage(70.0, page.GetRect().Height - image.GetHeight(), &image);
       
        painter.FinishDrawing();

        document.Save(filename);
    }

    catch (PdfError& e) {
        try {
            painter.FinishDrawing();
        }
        catch (...) {
      
        }
        throw e;
    }
}

int main() {
    try {
        const string pdfFilename = "C:\TestPoDoFo001.pdf";

        PrintLogo(pdfFilename);
    }
    catch (PdfError& err) {
        err.PrintErrorMsg();
        return static_cast<int>(err.GetCode());
    }

    cout << endl << "Created a PDF file: TestPoDoFo001.pdf" << endl << endl;

    return 0;
}

And I am obtaining the following compilation error:

Build started at 4:00 PM...
1>------ Build started: Project: PodofoTest1, Configuration: Release x64 ------
1>PodofoTest1.cpp
1>C:UsersFABIANESPINOsourcereposPodofoTest1PodofoTest1PodofoTest1.cpp(25,23): error C2665: 'PoDoFo::PdfImage::PdfImage': no overloaded function could convert all the argument types
1>C:vcpkginstalledx64-windowsincludepodofomainPdfImage.h(221,1):
1>could be 'PoDoFo::PdfImage::PdfImage(const PoDoFo::PdfImage &)'
1>  C:UsersFABIANESPINOsourcereposPodofoTest1PodofoTest1PodofoTest1.cpp(25,23):
1>  'PoDoFo::PdfImage::PdfImage(const PoDoFo::PdfImage &)': cannot convert argument 1 from 'PoDoFo::PdfMemDocument *' to 'const PoDoFo::PdfImage &'
1>      C:UsersFABIANESPINOsourcereposPodofoTest1PodofoTest1PodofoTest1.cpp(25,24):
1>      Reason: cannot convert from 'PoDoFo::PdfMemDocument *' to 'const PoDoFo::PdfImage'
1>C:vcpkginstalledx64-windowsincludepodofomainPdfImage.h(169,5):
1>or       'PoDoFo::PdfImage::PdfImage(PoDoFo::PdfObject &)'
1>  C:UsersFABIANESPINOsourcereposPodofoTest1PodofoTest1PodofoTest1.cpp(25,23):
1>  'PoDoFo::PdfImage::PdfImage(PoDoFo::PdfObject &)': cannot convert argument 1 from 'PoDoFo::PdfMemDocument *' to 'PoDoFo::PdfObject &'
1>C:UsersFABIANESPINOsourcereposPodofoTest1PodofoTest1PodofoTest1.cpp(25,23):
1>while trying to match the argument list '(PoDoFo::PdfMemDocument *)'
1>Done building project "PodofoTest1.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
========== Build completed at 4:00 PM and took 02.118 seconds ==========

Could you helping me showing the right way to include a PNG file using PoDoFo? I haven’t found much PoDoFo examples as starting point.

Kind regards.

2

Answers


  1. Chosen as BEST ANSWER

    Using the previous answer from @TheNomad:

    This code works:

    #include <iostream>
    #include <podofo/podofo.h>
    
    using namespace std;
    using namespace PoDoFo;
    
    void PrintLogo(const string_view& filename) {
        PdfMemDocument document;
        PdfPainter painter;
        PdfFont* font;
    
        string imageFile = "C:\image.png";
    
        try {
            auto& page = document.GetPages().CreatePage(PdfPage::CreateStandardPageSize(PdfPageSize::A4));
            painter.SetCanvas(page);
    
            font = document.GetFonts().SearchFont("Arial");
            if (!font) throw runtime_error("Invalid handle");
    
            painter.TextState.SetFont(*font, 18);
            painter.GraphicsState.SetFillColor(PdfColor(1, 0.5, 0.5));
            painter.GraphicsState.SetStrokeColor(PdfColor(1, 0.0, 0.0));
           
            auto image = document.CreateImage();
            image->Load(imageFile);
            painter.DrawImage(*image, 400.0, page.GetRect().Height - 50, 0.25, 0.25);
            painter.FinishDrawing();
    
            document.Save(filename);
        }
    
        catch (PdfError& e) {
            try {
                painter.FinishDrawing();
            }
            catch (...) {
          
            }
            throw e;
        }
    }
    
    int main() {
        try {
            const string pdfFilename = "C:\TestPoDoFo001.pdf";
    
            PrintLogo(pdfFilename);
        }
        catch (PdfError& err) {
            err.PrintErrorMsg();
            return static_cast<int>(err.GetCode());
        }
    
        cout << endl << "Created a PDF file: TestPoDoFo001.pdf" << endl << endl;
    
        return 0;
    }
    

  2. Seems I was looking in an older version which explains why the error seemed confusing. Indeed, like the examples you found online, the old one was PdfImage( PdfDocument* pParent, const char* pszPrefix = NULL ); with an optional parameter which is why image(&document); used to work.

    The updated PoDoFo PdfImage no longer has the old constructor with an optional second parameter. The new constructor is PdfImage(PdfDocument& doc, const std::string_view& prefix); and it’s marked as private. So what you’re trying shouldn’t work anymore.

    I haven’t worked with PoDoFo but a brief check of PdfDocument shows this:

    unique_ptr<PdfImage> PdfDocument::CreateImage(const string_view& prefix)
    {
        return unique_ptr<PdfImage>(new PdfImage(*this, prefix));
    }
    

    So I’d venture a guess that you need to call it from PdfDocument (or in your case, PdfMemDocument since it’s a child class) like so std::unique_ptr<PdfImage> image = document.CreateImage(prefix) (at least assuming you’re using the latest version). When using the returned value, keep in mind it returns a std::unique_ptr

    As I said I haven’t worked with this lib, so I can’t test and tell you whether your code will actually work, since I’d say that converting the old lib to C++17 changed it quite a lot (as you can see in the 2 different GitHub sources: the main and the old unmaintained fork), so that’ll be your homework and is an entirely different topic.

    But the above solution should fix the compilation error addressed by your question.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search