1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
/***************************************************************************
* Copyright (C) 2006 by Dominik Seichter *
* domseichter@web.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <podofo.h>
#include <fontconfig/fontconfig.h>
#include <cstdio>
using namespace PoDoFo;
#define MIN_PAGES 100
bool writeImmediately = true;
void AddPage( PdfDocument* pDoc, const char* pszFontName, const char* pszImagePath )
{
PdfPainter painter;
PdfPage* pPage;
PdfFont* pFont;
PdfFont* pArial;
PdfImage* pImage;
PdfRect rect;
try {
pFont = pDoc->CreateFont( pszFontName );
} catch ( const PdfError & e ) {
e.PrintErrorMsg();
return;
}
pPage = pDoc->CreatePage( PdfPage::CreateStandardPageSize( ePdfPageSize_A4 ) );
pArial = pDoc->CreateFont( "Arial" );
pImage = new PdfImage( pDoc );
rect = pPage->GetMediaBox();
const char* pszText = "The red brown fox jumps over the lazy dog!";
double dX = rect.GetLeft() + 20.0;
double dY = rect.GetBottom() + rect.GetHeight() - 20.0;
double dW, dH;
pFont->SetFontSize( 16.0 );
pArial->SetFontSize( 24.0 );
painter.SetPage( pPage );
painter.SetFont( pFont );
dW = pFont->GetFontMetrics()->StringWidth( pszText );
dH = -pFont->GetFontMetrics()->GetDescent(); // GetDescent is usually negative!
pFont->SetFontSize( 24.0 );
dH += pFont->GetFontMetrics()->GetLineSpacing() * 2.0;
painter.DrawRect( dX, dY, dW, dH );
dY -= pFont->GetFontMetrics()->GetLineSpacing();
painter.DrawText( dX, dY, "Hello World!" );
dY -= pFont->GetFontMetrics()->GetLineSpacing();
pFont->SetFontSize( 16.0 );
painter.DrawText( dX, dY, pszText );
painter.SetFont( pArial );
dY -= pArial->GetFontMetrics()->GetLineSpacing();
painter.DrawText( dX, dY, "The font used in this example is:" );
dY -= pArial->GetFontMetrics()->GetLineSpacing();
painter.DrawText( dX, dY, pszFontName );
dY -= pArial->GetFontMetrics()->GetLineSpacing();
#if defined PODOFO_HAVE_JPEG_LIB || defined PODOFO_HAVE_TIFF_LIB
try {
pImage->LoadFromFile( pszImagePath );
}
catch( const PdfError & e )
{
e.PrintErrorMsg();
}
dY -= (pImage->GetHeight() * 0.5);
dX = ((rect.GetWidth() - (pImage->GetWidth()*0.5))/2.0);
painter.DrawImage( dX, dY, pImage, 0.5, 0.5 );
delete pImage; // delete image right after drawing
#endif
painter.FinishPage();
}
void CreateLargePdf( const char* pszFilename, const char* pszImagePath )
{
PdfDocument* pDoc = NULL;
FcObjectSet* pObjectSet;
FcFontSet* pFontSet;
FcPattern* pPattern;
if( !FcInit() )
{
fprintf( stderr, "Cannot load fontconfig!\n");
return;
}
pPattern = FcPatternCreate();
pObjectSet = FcObjectSetBuild( FC_FAMILY, FC_STYLE, NULL );
pFontSet = FcFontList( 0, pPattern, pObjectSet );
FcObjectSetDestroy( pObjectSet );
FcPatternDestroy( pPattern );
if( writeImmediately )
pDoc = new PdfStreamedDocument( pszFilename );
else
pDoc = new PdfMemDocument();
if( pFontSet )
{
for( int i=0; i< (pFontSet->nfont > MIN_PAGES ? MIN_PAGES : pFontSet->nfont );i++ )
{
FcValue v;
//FcPatternPrint( pFontSet->fonts[i] );
FcPatternGet( pFontSet->fonts[i], FC_FAMILY, 0, &v );
//font = FcNameUnparse( pFontSet->fonts[i] );
printf(" -> Drawing with font: %s\n", reinterpret_cast<const char*>(v.u.s) );
AddPage( pDoc, reinterpret_cast<const char*>(v.u.s), pszImagePath );
}
FcFontSetDestroy( pFontSet );
}
if( writeImmediately )
static_cast<PdfStreamedDocument*>(pDoc)->Close();
else
static_cast<PdfMemDocument*>(pDoc)->Write( pszFilename );
delete pDoc;
}
void usage()
{
printf("Usage: LargetTest [-m] output_filename image_file\n"
" output_filename: filename to write produced pdf to\n"
" image_file: An image to embed in the PDF file\n"
"Options:\n"
" -m Build entire document in memory before writing\n"
"\n"
"Note that output should be the same with and without the -m option.\n");
}
int main( int argc, char* argv[] )
{
if( argc < 3 || argc > 4 )
{
usage();
return 1;
}
else if ( argc == 4)
{
// Handle options
// Is this argument an option?
if (argv[1][0] != '-')
{
usage();
return 1;
}
// Is it a recognised option?
if (argv[1][1] == 'm')
{
// User wants us to build the whole doc in RAM before writing it out.
writeImmediately = false;
++argv;
}
else
{
printf("Unrecognised argument: %s", argv[1]);
usage();
return 1;
}
}
try {
CreateLargePdf( argv[1], argv[2] );
printf("\nWrote the PDF file %s successfully\n", argv[1] );
} catch( PdfError & e ) {
e.PrintErrorMsg();
return e.GetError();
}
return 0;
}
|