+ Reply to Thread
Results 1 to 8 of 8

Thread: PDF to Image Converter

  1. #1
    Join Date
    Oct 2009
    Location
    London
    Posts
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Smile PDF to Image Converter

    Hi

    I'm working on PDF to Image Converter

    I made tool which to select Folder or Directory then It will convert each PDF file to an image..
    But I still have some problems.


    Qt Code:
    1. QString fileName = fileInfo.fileName();
    2.  
    3. QPixmap pdfFile;
    4. QPixmap scaledsimage;
    5. int page = 1;
    6. int large = 1000;
    7.  
    8. QFileInfo fixurl(lineEditSorceFolder->text()+"/"+fileName);
    9.  
    10.  
    11.  
    12. pdfFile = LoadPDF( fixurl.absoluteFilePath() ,page,large);
    13.  
    14.  
    15.  
    16. QByteArray bytes;
    17. QBuffer buffer(&bytes);
    18. buffer.open(QIODevice::ReadWrite);
    19. scaledsimage.save(&buffer,"PNG",100);
    To copy to clipboard, switch view to plain text mode 

    It's seem It dose the PDF.. but
    the image dose not come out at all..

    I have tested to draw something on the image and it dose work.

    Please, I need help.. I do not know if there is any easier way to Process that..

    Best Regards
    Rajab Natshah
    rajab.natshah.com

  2. #2
    Join Date
    Sep 2009
    Posts
    72
    Thanked 10 Times in 10 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: PDF to Image Converter

    HI

    http://www.foolabs.com/xpdf/ try this, u just have to port that in ur application. it will give u image also

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    6,437
    Thanks
    13
    Thanked 1,359 Times in 1,301 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: PDF to Image Converter

    I might be missing the obvious, but nothing in your code seems to set "scaledsimage", which will remain a null pixmap and produce nothing out at line 19.

  4. #4
    Join Date
    Oct 2009
    Location
    London
    Posts
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Smile Re: PDF to Image Converter

    Thank you ChrisW67 and vishwajeet.dusane

    I have been using ..GraphicsViewEdit example to get the PDF Opject Loaded then Save any page I want to a PNG file.
    http://www.qt-apps.org/content/show....?content=80234
    I have been using some code and Classes to my project.

    I have been messing with code.. this why it was not in my last code

    Qt Code:
    1. pdfFile = LoadPDF( fixurl.absoluteFilePath() ,page,large);
    2. QImage tImage = pdfFile.toImage();
    3. tImage.save("tfile.png","PNG",100);
    To copy to clipboard, switch view to plain text mode 

    The tool I'm working on is some sort of mass PDF Converter to Images.

    NatshahPDFtoImage.jpg

    I have been trying to make it some sample .. to convert the loaded PDF file into the

    the Function LoadPDF Returns QPixmap for the selected Page .. but it has no setScaledSize .. and when you save. no file at all shows up.

    Best Regards
    Rajab Natshah
    rajab.natshah.com

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    6,437
    Thanks
    13
    Thanked 1,359 Times in 1,301 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: PDF to Image Converter

    LoadPDF() is possibly not returning what you think. Have you tried looking at pdfFile.isNull() and/or pdfFile.size() to see if a meaningful pixmap is actually being returned?

    Alternatively, conversion to QImage is failing: use QImage::isNull() to check.

    Or, the save is failing but you are ignoring the return code.
    Last edited by ChrisW67; 4th March 2010 at 05:13. Reason: Fixed fat-handed typing

  6. #6
    Join Date
    Oct 2009
    Location
    London
    Posts
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Smile Re: PDF to Image Converter

    Hi ChrisW67

    Thanks for your help..

    the implementation Code for the Function LoadPDF

    Qt Code:
    1. extern inline QPixmap LoadPDF(QString fn, int Page, int w )
    2. {
    3. QString tmp, cmd1, cmd2;
    4. const QString pdfFile = PathConvert(fn);
    5. const QString tmpFile = PathConvert(QDir::homePath()+"/sctodaytmps.png");
    6. const QString qttmpFile = QDir::homePath()+"/sctodaytmps.png";
    7. QPixmap pm;
    8. tmp.setNum(Page);
    9. int ret = -1;
    10. tmp.setNum(Page);
    11. args.append("-sDEVICE=png16m");
    12. args.append("-r72");
    13. args.append("-dGraphicsAlphaBits=4");
    14. args.append("-o");
    15. args.append(tmpFile);
    16. args.append("-dFirstPage="+tmp);
    17. args.append("-dLastPage="+tmp);
    18. args.append(pdfFile);
    19. ret = callGS(args);
    20. ////////qDebug() << "### ret " << ret;
    21. if (ret == 0)
    22. {
    23. QPixmap tmpimage(qttmpFile);
    24. QPixmap penna = tmpimage.scaledToWidth(w);
    25. tmpimage.detach();
    26. QFile lastaction(qttmpFile);
    27. lastaction.remove();
    28. p.begin(&penna);
    29. p.setBrush(Qt::NoBrush);
    30. p.setPen(QPen(QBrush(Qt::black),2,Qt::SolidLine));
    31. p.drawRect(0, 0, penna.width(), penna.height());
    32. p.end();
    33. return penna;
    34. }
    35. return pm;
    36. }
    To copy to clipboard, switch view to plain text mode 



    I have tested the way you suggested with this code

    Qt Code:
    1. pdfFile = LoadPDF( fixurl.absoluteFilePath() ,page,large);
    2.  
    3. if (pdfFile.isNull())
    4. QMessageBox::warning(0,"pdfFile.isNull() = True ","pdfFile.isNull() = True");
    5.  
    6. QImage tImage = pdfFile.toImage();
    7. if (tImage.isNull())
    8. QMessageBox::warning(0,"tImage.isNull() = True ","tImage.isNull() = True");
    To copy to clipboard, switch view to plain text mode 

    I'm getting both of the messages .. that mean it's not loading and converting to QPixmap

    I think I will miss with the function more tomorrow..

    Best Regards
    Rajab Natshah
    rajab.natshah.com

  7. #7
    Join Date
    Aug 2010
    Posts
    1
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default Re: PDF to Image Converter

    Hi Rajab,
    i'm interested in a function that converts PDF in a QImage, how can i implement it? is ur code available on sourceforge or somewhere?? tnx for ur help

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    6,437
    Thanks
    13
    Thanked 1,359 Times in 1,301 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: PDF to Image Converter

    The approach above uses Ghostscript to convert the PDF to PNG. It simply builds a command line and runs gs (using QProcess I assume) to do the conversion. The resulting PNG file is then loaded into a QPixmap/QImage.

  9. The following user says thank you to ChrisW67 for this useful post:

    marsiliano (10th August 2010)

+ Reply to Thread

Similar Threads

  1. Replies: 6
    Last Post: 21st September 2009, 11:55
  2. hide image,once moused moved from image
    By yuvaraj.yadav in forum Qt Programming
    Replies: 1
    Last Post: 22nd April 2009, 09:16
  3. Unit Converter Widget
    By baray98 in forum Qt Programming
    Replies: 11
    Last Post: 8th January 2008, 15:43
  4. Replies: 3
    Last Post: 14th March 2007, 09:09
  5. Image Converter
    By deekayt in forum General Discussion
    Replies: 1
    Last Post: 30th October 2006, 22:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts