Générer un fichier PDF avec QPrint
Bonjour tout le monde,
J'ai des données récupérées dans un QStringList que je veux mettre dans un fichier PDF. j'utilise le code suivant :
Code:
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
| if(pdfFile.isEmpty())
return;
int nbColonnes = header.size();
int nbDonnees = donnees.size();
QPrinter printer; //The QPrinter class is a paint device that paints on a printer
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOrientation(QPrinter::Landscape);
printer.setPageSize(QPrinter::A4);
//this can be also NativeFormat or PostScriptFormat
//for details, read QPrinter class documentation
printer.setOutputFileName(pdfFile);
QPainter painter; //The QPainter class performs painting on widgets and other paint devices (such as QPrinter)
//here we start painting to the printer
if (!painter.begin(&printer))
{
QMessageBox::critical(this, NAME_APP, "Fichier pdf non crée");
return;
}
int x = 0;
int y = 0;
QRect r;
for (int k=0; k<nbColonnes; k++)
{
// bilog-mh cf. TL-20819 : Paraméterer largeur des colonnes à afficher
if (k==0)
colonne_width = 70;
else if (k==1)
colonne_width = 130;
else if (k==2)
colonne_width = 150;
else if (k==3)
colonne_width = 700;
QRect required = QRect(); //this represent the required rectangled size
r = QRect(x, 0, colonne_width, 60); //this represent our calculated rectangle size
painter.drawRect(r);
//now we insert each string of the list into the rectangle
QString text = header.at(k);
//now we draw the text into the given rectangle, using word wrap option.
//the last parameter indicates a rectangle in which the text should be enclosed
painter.drawText(r, Qt::AlignCenter | Qt::TextWordWrap, text, &required);
//if the calculated height is not enought for drawing the text, we should redraw all rectangles
x += colonne_width;
}
x = 0;
for (int k=0; k<nbDonnees; k++)
{
// bilog-mh cf. TL-20819 : Paraméterer largeur des colonnes à afficher
switch ((k+1)%4)
{
case 1 :
colonne_width = 70;
break;
case 2:
colonne_width = 130;
break;
case 3 :
colonne_width = 150;
break;
case 0 :
colonne_width = 700;
break;
}
if (k%nbColonnes == 0)
{
// bilog-mh cf. TL-20819 : si première ligne de données alors on retient le height de la ligne header
if (k== 0)
y += 60;
else
y += 200;
x = 0;
}
QRect required = QRect(); //this represent the required rectangled size
r = QRect(x, y, colonne_width, 200); //this represent our calculated rectangle size
painter.drawRect(r);
//now we insert each string of the list into the rectangle
QString txt = donnees.at(k);
//now we draw the text into the given rectangle, using word wrap option.
//the last parameter indicates a rectangle in which the text should be enclosed
painter.drawText(r, Qt::AlignJustify | Qt::TextWordWrap, txt, &required);
x += colonne_width;
// bilog-mh cf. TL-20819 : si on atteint la fin de page, on insère un nouvelle page
if (y > printer.height())
printer.newPage();
}
painter.end(); |
Le problème c'est que les données ne sont affichées que dans la 1ère page et les autres pages sont vides. Le printer.newPage() crée bien les pages mais elles sont vides.
Est ce que quelqu'un pourrait m'aider à résoudre ce problème ?
Merci d'avance pour votre collaboration
Bien cordialement