Results 1 to 17 of 17

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
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 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
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 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 04: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
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 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)

  10. #9

    Default Re: PDF to Image Converter

    Hi, thanks for sharing converting PDF to PNG. I just wonder whether it works for other raster images, especially for PDF to TIFF conversion, single-page or multi-page. Is that OK?
    Do one thing at a time, and do well.

  11. #10

    Default Re: PDF to Image Converter

    Quote Originally Posted by mindylynn0 View Post
    Hi, thanks for sharing converting PDF to PNG. I just wonder whether it works for other raster images, especially for PDF to TIFF conversion, single-page or multi-page. Is that OK?


    Hi, mindylynn0.
    Have you ever found your way out?

  12. #11
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,239
    Thanks
    303
    Thanked 865 Times in 852 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: PDF to Image Converter

    Considering she made only one post to this forum, and that one nearly two years ago, it is very unlikely you will get an answer to your question from her. Why bring back a dead thread? Check the date of the last post before you respond to something.

  13. #12
    Join Date
    Jan 2017
    Posts
    1
    Qt products
    Qt3
    Platforms
    MacOS X

    Default Re: PDF to Image Converter

    To save PDF to images for easily sharing, viewing and others, PDFelement is what you need. It converts PDF pages into images like JPG, JPEG, PNG, BMP, TIFF, etc. What's more, the images in PDF file can be exported to your computer.

    youtube.com/watch?v=F67uHRaNvJc

  14. #13
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,239
    Thanks
    303
    Thanked 865 Times in 852 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: PDF to Image Converter

    Maybe you can explain why you think posting a link to a video about a commercial product is relevant in a forum dedicated to writing software using Qt? And why we shouldn't just consider your post to be spam?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  15. #14
    Join Date
    Jan 2024
    Posts
    1
    Platforms
    Windows Android

    Default Re: PDF to Image Converter

    Quote Originally Posted by marsiliano View Post
    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 helpstory saver instagram
    Hello! Unfortunately, my code isn't on SourceForge. However, I can guide you in implementing a function to convert PDF to QImage. Feel free to share more details about your project!

  16. #15
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,239
    Thanks
    303
    Thanked 865 Times in 852 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: PDF to Image Converter

    Feel free to share more details about your project!
    Do you realize you are replying to a 14 year old post?

    In case anyone wanders into this post, the QPdf module contains the functionality needed to open a PDF document, navigate through it page by page, and render individual pages to a QImage using QPdfDocument. If you want to save the image to a file, QImage::save() will do that. If you want to display the page image on-screen, then QPainter::drawImage() will do that for you.

    There is no need for any external (non-Qt) code to accomplish this.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  17. #16
    Join Date
    May 2024
    Location
    India
    Posts
    4
    Qt products
    Qt/Embedded
    Platforms
    Windows

    Default Re: PDF to Image Converter

    Hey hi, you should check the code because scaledsimage isn't being set correctly.

    Qt Code:
    1. pdfFile = LoadPDF(fixurl.absoluteFilePath(), page, large);
    2. if (pdfFile.isNull()) {
    3. QMessageBox::warning(0, "Error", "Failed to load PDF.");
    4. } else {
    5. QImage tImage = pdfFile.toImage();
    6. if (!tImage.isNull()) {
    7. tImage.save("output.png", "PNG", 100);
    8. } else {
    9. QMessageBox::warning(0, "Error", "Failed to convert QPixmap to QImage.");
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

  18. #17
    Join Date
    May 2024
    Location
    India
    Posts
    4
    Qt products
    Qt/Embedded
    Platforms
    Windows

    Default Re: PDF to Image Converter

    Quote Originally Posted by nehakakar View Post
    Hey hi, you should check the code because scaledsimage isn't being set correctly.

    Qt Code:
    1. pdfFile = LoadPDF(fixurl.absoluteFilePath(), page, large);
    2. if (pdfFile.isNull()) {
    3. QMessageBox::warning(0, "Error", "Failed to load PDF.");
    4. } else {
    5. QImage tImage = pdfFile.toImage();
    6. if (!tImage.isNull()) {
    7. tImage.save("output.png", "PNG", 100);
    8. } else {
    9. QMessageBox::warning(0, "Error", "Failed to convert QPixmap to QImage.");
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 
    On the other hand you can also use any online image converter such as Image Converter and for compressor Jpeg Compressor, these are really good tool.

Similar Threads

  1. Unit Converter Widget
    By baray98 in forum Qt Programming
    Replies: 13
    Last Post: 17th December 2016, 16:06
  2. Replies: 6
    Last Post: 21st September 2009, 10:55
  3. hide image,once moused moved from image
    By yuvaraj.yadav in forum Qt Programming
    Replies: 1
    Last Post: 22nd April 2009, 08:16
  4. Replies: 3
    Last Post: 14th March 2007, 08:09
  5. Image Converter
    By deekayt in forum General Discussion
    Replies: 1
    Last Post: 30th October 2006, 21: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.