ahmedb
7th November 2013, 18:38
I'm trying to print an Arabic text to PDF file and to a normal printer using QPrinter. I face two different problems.
First, when printing to PDF, text appears as if cracked; with spaces between characters (letters) that should be there.
Second, when printing to a normal printer the text appears correct, but with wrong text alignment !
printing code:
void Widget::printDocument(QPainter &painter, QPrinter &printer)
{
qint32 margin = 40 ;
QRect rct = printer.paperRect().adjusted(margin, margin, -margin, -margin) ;
painter.drawRect(rct) ;
QTextOption toption ;
toption.setTextDirection(Qt::RightToLeft) ;
QFont fontTitle("Arial", 36) ;
fontTitle.setBold(true);
painter.setFont(fontTitle);
QString title = ui->leTitle->text().trimmed() ;
toption.setAlignment(Qt::AlignTop|Qt::AlignHCenter );
painter.drawText(rct.adjusted(0, 10, 0, 0), title, toption);
QFont fontText("Arial", 28) ;
painter.setFont(fontText) ;
QString text = ui->teText->toPlainText() ;
toption.setAlignment(Qt::AlignLeft|Qt::AlignHCente r);
painter.drawText(rct.adjusted(0, 10+painter.fontMetrics().height()*4, 0, 0), text, toption);
}
void Widget::on_pbPDF_clicked()
{
QString fileName = QFileDialog::getSaveFileName(this, QString(), QString(), QString("*.pdf(*.pdf)")) ;
if(fileName.isEmpty())
return ;
QPrinter printer ;
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName(fileName);
QPainter painter(&printer) ;
printDocument(painter, printer) ;
painter.end() ;
QDesktopServices::openUrl(QUrl(fileName)) ;
}
system information:
- tried both Windows 7 64bit, and Windows XP (with Qt 4.8)
- the printer: tried both HP 1018, and HP Laserjet P1005
- Qt version: tried 4.8, and 5.1 both has the same problem.
Note: attached a sample qt project that shows the problem. and the resulting pdf .
First, when printing to PDF, text appears as if cracked; with spaces between characters (letters) that should be there.
Second, when printing to a normal printer the text appears correct, but with wrong text alignment !
printing code:
void Widget::printDocument(QPainter &painter, QPrinter &printer)
{
qint32 margin = 40 ;
QRect rct = printer.paperRect().adjusted(margin, margin, -margin, -margin) ;
painter.drawRect(rct) ;
QTextOption toption ;
toption.setTextDirection(Qt::RightToLeft) ;
QFont fontTitle("Arial", 36) ;
fontTitle.setBold(true);
painter.setFont(fontTitle);
QString title = ui->leTitle->text().trimmed() ;
toption.setAlignment(Qt::AlignTop|Qt::AlignHCenter );
painter.drawText(rct.adjusted(0, 10, 0, 0), title, toption);
QFont fontText("Arial", 28) ;
painter.setFont(fontText) ;
QString text = ui->teText->toPlainText() ;
toption.setAlignment(Qt::AlignLeft|Qt::AlignHCente r);
painter.drawText(rct.adjusted(0, 10+painter.fontMetrics().height()*4, 0, 0), text, toption);
}
void Widget::on_pbPDF_clicked()
{
QString fileName = QFileDialog::getSaveFileName(this, QString(), QString(), QString("*.pdf(*.pdf)")) ;
if(fileName.isEmpty())
return ;
QPrinter printer ;
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName(fileName);
QPainter painter(&printer) ;
printDocument(painter, printer) ;
painter.end() ;
QDesktopServices::openUrl(QUrl(fileName)) ;
}
system information:
- tried both Windows 7 64bit, and Windows XP (with Qt 4.8)
- the printer: tried both HP 1018, and HP Laserjet P1005
- Qt version: tried 4.8, and 5.1 both has the same problem.
Note: attached a sample qt project that shows the problem. and the resulting pdf .