cutycapt does not create links inside the pdf - qt

I am using CutyCapt to create pdf from html, the issue is that the pdf does not contain links.
it removes all the links from the pdf. now I saw in that code that it creates object from type
QWebFrame and then print that frame to the printer.
QWebFrame *mainFrame = mPage->mainFrame();
QPainter painter;
case PdfFormat:
case PsFormat: {
QPrinter printer;
printer.setPageSize(QPrinter::A4);
printer.setOutputFileName(mOutput);
// TODO: change quality here?
mainFrame->print(&printer);
break;
}
Any idea, what I need to do on the QT side to enable links .

Related

Print out each content in a separate page use QTextdocument in Qt C++

I am a newbie in Qt c++.
I use QTextdocment to print PDF format.
My QTextdocment (doc) is quite long, it consists of separate content. So how can I print out each content in a separate page.
QPrinter printer(QPrinter::HighResolution);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setPaperSize(QPrinter::A4);
printer.setOrientation(QPrinter::Landscape);
printer.setOutputFileName(ui->lE_Save->text()+ ".pdf");
doc.setDocumentMargin(0);
doc.setTextWidth(0.5);
doc.print(&printer);
Thank you.
Thank to all of you.
I got the solution to export the contents page by page in PDF format.
My code something like that:
QPrinter printer(QPrinter::HighResolution);
printer.setOutputFormat(QPrinter::PdfFormat);
QPageSize pageSize(QPageSize::A4);
QPainter painter;
painter.begin(&printer);
for...
{
QTextDocument doc;
QTextCursor cursor(&doc);
...do something for QTextDocument ...
doc.drawContents(&painter);
if(page!=monthCount-1)
printer.newPage();
}
painter.end();

How do I modify string from clipboard before paste in a TextFIeld?

The TextField type (I'm using QML, but shouldn't matter) provides a paste() method. However, I would like to be able to strip all white space from the clipboard data before the paste happens.
There doesn't seem to be any hooks provided by Qt to do this in a simple fashion. Does anyone know how to achieve this?
You can expose a C++ function that modifies the clipboard data and returns it to QML:
class ClipboardModifier : public QObject
{
// ...
Q_INVOKABLE QString modifiedClipboardText() const
{
QClipboard *clipboard = QGuiApplication::clipboard();
QString clipboardText = clipboard->text();
// modify the text...
return clipboardText;
}
}
Or just make a simple wrapper of QClipboard as shown in the answers here, and do the modification in QML with JavaScript and then set the text on the text field directly.

Printing Entire QWebView Content does not result as expected

I have been trying to get the QWebView printed. Initially it looked simple.
class myView:public QWebView
{
...
}
In one of the public Member Function of myView
QPrinter printer;
printer.setPageSize(QPrinter::A4);
printer.setOrientation(QPrinter::Portrait);
QPrintDialog *dlog = new QPrintDialog (&printer, this);
qDebug () <<" HoHoooo.. Going to print ";
if ( dlog->exec () != QDialog::Accepted)
{
qDebug () <<"Printing Not accepted.. :( ";
}
else
{
print(&printer);
}
The output for the print is not getting properly. Kindly suggest, if i miss something?
Check the HTML code you want to print if it conforms to the HTML specification (use a HTML-checking tool!), even though your code renders fine to the screen, print-rendering is totally different code and thus can behave differently in case your HTML page does not conform to the standard.
In case it conforms, you can check printing in Safari or any other WebKit based browser to see, if it prints correctly there.

QPrintPreviewDialog incorrect preview

Using QPrintPreviewDialog to preview the print, I use the following code
QPrinter printer;
printer.setResolution(QPrinter::HighResolution);
printer.setPaperSize(QPrinter::A4);
printer.setOrientation(QPrinter::Portrait);
QPrintPreviewDialog *pd = new QPrintPreviewDialog(&printer);
connect(pd,SIGNAL(paintRequested(QPrinter*)),this,SLOT(print(QPrinter*)));
pd->exec();
void Class::print(QPrinter *p)
{
QTextEdit *ted = new QTextEdit;
ted->insertHtml("<center><img src='"+QString(":/img/logo.png")+"' width='90' height='72'/><b><font size='9'>Logo Text</font></b></center>");
ted->document()->print(p);
}
On pushing the print button, this dialog appears:
As you can see the content is spread all over the page.
Then I click the page setup button on the preview dialog and this appears:
without changing anything, I click OK and then the preview becomes correct:
The question is that how to correct the preview by code?
Use QTextDocument instead of a QTextEdit, the latter is a widget, which makes the output depend on resizing.
Add a QPageSetupDialog to show before preview.
I had the same issue. Apparently, pressing the OK button of the page setup dialog changes the resolution. To fix this, I change the resolution back in the method which calculates the print preview:
dialog = QPrintPreviewDialog()
dialog.paintRequested.connect(self.print)
dialog.exec_()
def print(self, printer):
printer.setResolution(300)
painter = QPainter()
painter.begin(printer)
...

Can I print an HTMLLoader (pdf) in Adobe Air?

I'm using AlivePDF to create a PDF file, then save it to the desktop. I can then use an HTMLLoader to display my lovely PDF file.
Now, the print button in Adobe Reader works fine. However, there will be young children using the app, so I'd like to have a big "Print" button right above it.
I figured I could just start up a print job and feed it my HTMLLoader. This won't work because the HTML loader rasterizes the content.
Any suggestions?
One answer I have found in order to solve this problem is called Cross-scripting PDF content. The idea is that a PDF can have embedded JavaScript, which can be called from the JavaScript within the HTML page "housing" said PDF (object tag only, no embed).
This site was of particular help. I had to simplify the JavaScript from that page down quite a bit. I kept getting syntax errors.
I also need my program to generate the PDF and the HTML content. I cannot ship a single PDF with embedded JS and an HTML file pointing to it. They need to be dynamically generated by the user. Here is a basic rundown:
private function printText(text:String):void
{
var p:PDF=new PDF(Orientation.PORTRAIT, Unit.MM, Size.LETTER);
p.addPage();
p.addText(text, 100, 100);
p.addJavaScript(this.getJavascript());
var f:FileStream=new FileStream();
var html:File=File.desktopDirectory.resolvePath("exported.html");
f.open(html, FileMode.WRITE);
f.writeUTF(this.getHtml());
f.close();
var file:File=File.desktopDirectory.resolvePath("exported.pdf");
f.open(file, FileMode.WRITE);
var bytes:ByteArray=p.save(Method.LOCAL);
f.writeBytes(bytes);
f.close();
Now that we have our two files, HTML and PDF, we can view the PDF, and create a giant purple print button for our younger / sight-impared users.
if (HTMLLoader.pdfCapability == HTMLPDFCapability.STATUS_OK)
{
var win:PrintTitleWindow; //the window w/giant button
var htmlLoader:HTMLLoader=new HTMLLoader();
var url:URLRequest=new URLRequest(html.url);
htmlLoader.width=880;
htmlLoader.height=(appHeight - 150); //i figure out the height elsewhere
htmlLoader.load(url);
var holder:UIComponent=new UIComponent();
holder.addChild(htmlLoader);
win=PrintTitleWindow(PopUpManager.createPopUp(mainWindow, PrintTitleWindow, true));
win.width=900;
win.height=(appHeight - 50);
win.addChild(holder);
win.addContent(htmlLoader);
PopUpManager.centerPopUp(win);
}
}
Here is the JS and HTML I used. I'm adding these in here for laughs. I'm sure there is a better way to do this, but I'm tired and it is late.
private function getJavascript():String
{
return 'function myOnMessage(aMessage) { print({ bUI: true, bSilent: false, bShrinkToFit: true }); } function myOnDisclose(cURL,cDocumentURL) { return true; } function myOnError(error, aMessage) { app.alert(error); } var msgHandlerObject = new Object(); msgHandlerObject.onMessage = myOnMessage; msgHandlerObject.onError = myOnError; msgHandlerObject.onDisclose = myOnDisclose; this.hostContainer.messageHandler = msgHandlerObject;';
}
private function getHtml():String
{
return '<html><head><script>function callPdfFunctionFromJavascript(arg) { pdfObject = document.getElementById("PDFObj");pdfObject.postMessage([arg]);}</script></head><body><object id="PDFObj" data="exported.pdf" type="application/pdf" width="100%" height="100%"></object></body></html>';
}
The PrintTitleWindow is a simple title window with the print button on it. The code to print is simple.
myHtmlLoader.window.callPdfFunctionFromJavascript('Print');
Eh Voila! I have a gigantor-print-button like so:
(source: cetola.net)
Hitting that big purple print button is the same as hitting the print icon in the PDF toolbar. The difference for me is that my users, who could be elementary or middle-school kids, won't have to look around for the stupid button.
So, it's a long way around the block. Still, if you need to print and can't rely on that adobe toolbar button, here's your answer :) .

Resources