PDA

View Full Version : PDF to Image Converter



RajabNatshah
3rd March 2010, 15:24
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.




QString fileName = fileInfo.fileName();

QPixmap pdfFile;
QPixmap scaledsimage;
int page = 1;
int large = 1000;

QFileInfo fixurl(lineEditSorceFolder->text()+"/"+fileName);



pdfFile = LoadPDF( fixurl.absoluteFilePath() ,page,large);



QByteArray bytes;
QBuffer buffer(&bytes);
buffer.open(QIODevice::ReadWrite);
scaledsimage.save(&buffer,"PNG",100);


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 :)

vishwajeet.dusane
3rd March 2010, 18:59
HI

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

ChrisW67
3rd March 2010, 23:15
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.

RajabNatshah
4th March 2010, 00:13
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.php/GraphicsViewEdit+Layer?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



pdfFile = LoadPDF( fixurl.absoluteFilePath() ,page,large);
QImage tImage = pdfFile.toImage();
tImage.save("tfile.png","PNG",100);


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

4368

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 :)

ChrisW67
4th March 2010, 05:08
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.

RajabNatshah
5th March 2010, 05:06
Hi ChrisW67

Thanks for your help..

the implementation Code for the Function LoadPDF



extern inline QPixmap LoadPDF(QString fn, int Page, int w )
{
QString tmp, cmd1, cmd2;
const QString pdfFile = PathConvert(fn);
const QString tmpFile = PathConvert(QDir::homePath()+"/sctodaytmps.png");
const QString qttmpFile = QDir::homePath()+"/sctodaytmps.png";
QPixmap pm;
tmp.setNum(Page);
int ret = -1;
tmp.setNum(Page);
QStringList args;
args.append("-sDEVICE=png16m");
args.append("-r72");
args.append("-dGraphicsAlphaBits=4");
args.append("-o");
args.append(tmpFile);
args.append("-dFirstPage="+tmp);
args.append("-dLastPage="+tmp);
args.append(pdfFile);
ret = callGS(args);
////////qDebug() << "### ret " << ret;
if (ret == 0)
{
QPixmap tmpimage(qttmpFile);
QPixmap penna = tmpimage.scaledToWidth(w);
tmpimage.detach();
QFile lastaction(qttmpFile);
lastaction.remove();
QPainter p;
p.begin(&penna);
p.setBrush(Qt::NoBrush);
p.setPen(QPen(QBrush(Qt::black),2,Qt::SolidLine));
p.drawRect(0, 0, penna.width(), penna.height());
p.end();
return penna;
}
return pm;
}




I have tested the way you suggested with this code



pdfFile = LoadPDF( fixurl.absoluteFilePath() ,page,large);

if (pdfFile.isNull())
QMessageBox::warning(0,"pdfFile.isNull() = True ","pdfFile.isNull() = True");

QImage tImage = pdfFile.toImage();
if (tImage.isNull())
QMessageBox::warning(0,"tImage.isNull() = True ","tImage.isNull() = True");


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 :)

marsiliano
9th August 2010, 17:08
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

ChrisW67
10th August 2010, 00:27
The approach above uses Ghostscript (http://pages.cs.wisc.edu/~ghost/) 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.

mindylynn0
27th October 2014, 09:30
Hi, thanks for sharing converting PDF to PNG (http://www.pqscan.com/convert-pdf/to-png-csharp.html). I just wonder whether it works for other raster images, especially for PDF to TIFF conversion (http://www.pqscan.com/convert-pdf/to-tiff-csharp.html), single-page or multi-page. Is that OK?

peterlee
20th February 2016, 06:40
Hi, thanks for sharing converting PDF to PNG (http://www.pqscan.com/convert-pdf/to-png-csharp.html). I just wonder whether it works for other raster images, especially for PDF to TIFF conversion (http://www.pqscan.com/convert-pdf/to-tiff-csharp.html), single-page or multi-page. Is that OK?

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

d_stranz
20th February 2016, 17:08
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.

BonziRafer
23rd January 2017, 07:07
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

d_stranz
25th January 2017, 01:08
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?

Kaizien48
7th January 2024, 10:10
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 (https://storysaver.page/)
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!

d_stranz
12th January 2024, 01:32
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.