Important: Please read the Qt Code of Conduct -

Sharing pdf file via email

  • QT Forum: Attach PDF Email Issue

    I'm writing a function in QT to generate a pdf file and share the file via email. On iOS I am encountering an issue where a pdf file gets generated and attached to an email and sent successfully, but the received email does not contain the original pdf file for some reason.

    The pdf file got generated as below returns true:

    qDebug() << QFile(attachedFile).exists();

    I was able to see the file got attached to an email and sent it. I was able to receive the email sent. However, when I opened up the email it did not contain the file attached.

    Here is my implementation.

    /* Constants */
    #define FILE_NAME "test.pdf"
    #include <QtWidgets>

    void ShareSheetWidget::open(QString attachedFile)

    /* added iOS specific */
    #ifdef _WIN32
    #elif __APPLE__
        #if TARGET_OS_IPHONE
    /* Compute system directory from name */
    QString path = QStandardPaths::standardLocations(QStandardPaths::DataLocation).value(0);
    QDir dir(path);
    if (!dir.exists())
    if (!path.isEmpty() && !path.endsWith("/"))
    attachedFile = path+"/"+FILE_NAME;
    qDebug() << "attachedFile= " + attachedFile;
    QFile f(attachedFile);
    if( {
        QPdfWriter* writer = new QPdfWriter(&f);
        QPainter* p = new QPainter(writer);
        p->drawText(QRect(100, 100, 2000, 200), "test");
        p->drawText(QRect(100, 100, 2000, 200), "test");
        delete p;
        delete writer;
    qDebug() << QFile(attachedFile).exists();
    if(QFile::exists(attachedFile)) {
    else {
        qDebug() << "File not exists: " + attachedFile;


    void ShareSheetWidget::share(QString attachedFile)
    /* added iOS specific /
    #ifdef _WIN32
    #elif APPLE
    mailer = new Mailer(this);
    QString subject = "Sharing Master Rig";
    QList<QString> recepients;
    recepients << “";
    QString body = "See attached file";
    mailer->open(subject, recepients, body, attachedFile);

    The mailer class I'm referencing is written in Xcode and can be found here.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You should also try to contact the author of the module. He might be able to help you since he wrote that code.

Log in to reply