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
Using the previous answer from @TheNomad:
This code works:
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 whyimage(&document);
used to work.The updated PoDoFo
PdfImage
no longer has the old constructor with an optional second parameter. The new constructor isPdfImage(PdfDocument& doc, const std::string_view& prefix);
and it’s marked asprivate
. So what you’re trying shouldn’t work anymore.I haven’t worked with PoDoFo but a brief check of
PdfDocument
shows this: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 sostd::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 astd::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.