Home » U++ Library support » Draw, Display, Images, Bitmaps, Icons » PDF file from Report
PDF file from Report [message #56414] |
Tue, 09 March 2021 13:50  |
mubeta
Messages: 77 Registered: October 2006
|
Member |
|
|
Hi all,
using PdfDraw object for export an multiplepages report in PDf file result in a very big file when there are images. For reduce the size of the resulting PDF file, I modified the method PdfDraw::DrwImageOp(...) in this way:
void PdfDraw::DrawImageOp(int x, int y, int cx, int cy, const Image& _img, const Rect& src, Color c)
{
Image img = _img;
if(!IsNull(c))
img = CachedSetColorKeepAlpha(img, c);
Tuple2<int64, Rect> key = MakeTuple(img.GetSerialId(), src);
int q = images.Find(key);
if(q < 0)
{
//q = images.GetCount();
//images.Add(key, img);
Image h = sJPEGDummy();
q = images.GetCount();
images.Add(key, h);
data = JPGEncoder(40).SaveString(img);
img = h;
}
if(img.GetSerialId() == sJPEGDummy().GetSerialId())
jpeg.Add(data);
page << "q "
<< Pt(cx) << " 0 0 " << Pt(cy) << ' '
<< Pt(x) << ' ' << Pt(pgsz.cy - y - cy)
<< " cm /Image" << q + 1 << " Do Q\n";
}
This will convert every images in compressed JPEG, that in fact result in a smaller PDF file. But, I'm not sure that when the same image is repeated multiple times in the report, the PDF file will contain this image only once or multiple times. It seems to me that the Tuple2 key is intended for this scope, but in fact if I export a test report with only the same image for mulitple pages, without other content, the file size increase proportionally with the page numbers.
What I'm missing?
Many thanks!!
|
|
|
Re: PDF file from Report [message #56424 is a reply to message #56414] |
Wed, 10 March 2021 09:25   |
mubeta
Messages: 77 Registered: October 2006
|
Member |
|
|
I solved it by changing some more code, generating the tuple key by the image hash. In this way the PDF file size decrease a lot, similar to using a PDF printer.
void PdfDraw::DrawImageOp(int x, int y, int cx, int cy, const Image& _img, const Rect& src, Color c)
{
Image img = _img;
if(!IsNull(c))
img = CachedSetColorKeepAlpha(img, c);
//Tuple2<int64, Rect> key = MakeTuple(img.GetSerialId(), src);
Tuple2<int64, Rect> key = MakeTuple(img.GetHashValue(), src);
int q = images.Find(key);
if(q < 0)
{
//q = images.GetCount();
//images.Add(key, img);
Image h = sJPEGDummy();
q = images.GetCount();
images.Add(key, h);
data = JPGEncoder(40).SaveString(img);
img = h;
}
if(img.GetSerialId() == sJPEGDummy().GetSerialId())
jpeg.Add(data);
page << "q "
<< Pt(cx) << " 0 0 " << Pt(cy) << ' '
<< Pt(x) << ' ' << Pt(pgsz.cy - y - cy)
<< " cm /Image" << q + 1 << " Do Q\n";
}
[Updated on: Wed, 10 March 2021 09:25] Report message to a moderator
|
|
|
|
|
Re: PDF file from Report [message #56427 is a reply to message #56426] |
Wed, 10 March 2021 21:02   |
 |
mirek
Messages: 14258 Registered: November 2005
|
Ultimate Member |
|
|
mubeta wrote on Wed, 10 March 2021 18:46Maybe DrawJPEG can be used for adding some additional pictures to the PDF, (that is in fact all what can be understood from the available examples), but the U++ library itself don't foreseen nothing to configure PdfDraw to export PDF with JPG images and optimize the PDF size. When generating the PDF from an QTF text, the resulting file will be very, very fat for nothing.
thanks anyway.
Well, the problem with Jpeg is that it is loss compression, so we basically cannot do that. That BTW is the reason why the argument of DrawJPEG is String, because that way you can put original JPEG file there directly.
Anyway, improving QTF -> PDF with respect to JPG is a good idea. Almost all is there already, missing one last step...
|
|
|
|
Goto Forum:
Current Time: Tue May 13 17:52:07 CEST 2025
Total time taken to generate the page: 0.01476 seconds
|