Can any one tell me how to write a simple C++ code to export my data(from variable) into PDF file without using any external libraries or utilities?
views:
318answers:
5...without using any external libraries or utilities?
that is a bit of a problem... If you are using QT you can do it... What is your setup?
Use fprintf
and write to a file, conforming to the PDF file format.
Specifically look in Appendix G. You might be able to get away with some very simple transformations and produce a readable PDF file.
Without using a PDF library your best way might be to create a simple PDF with sample text using some other application (eg OpenOffice). Then use this as a basis for the boiler plate and have you C++ code insert your own text in place. Note that PDF is line orientated so each new line of text has to be explicitly placed at the correct position.
For anything other than very simple output this is going to be tricky without fully understanding the PDF spec
Can any one tell me how to write a simple C++ code to export my data(from variable) into PDF file without using any external libraries or utilities?
No. PDF files are complicated. To read and write them, your code is going to be complicated. Normally, you could use an external library to hide that complexity, but if you don't want to do that, then your code is going to have to contain the complexity. And then it will not be simple.
You're asking for the impossible. You're asking to perform a complex task without doing anything complex yourself, and without relying on anyone else to do something complex for you. But someone is going to have to do it.
I'm currently using ZendPDF right now, and I have created a test pdf file that looked like this when viewed as text. When I changed the text "This is a test" to "This is a super test" in the pdf file itself, I was able to change the final pdf file and it opened in foxit. So, copy this and use C++ to replace the string with what you want. I was also able to make new text appear with just a text editor by copying that F1 14 Tf stuff.
The php used to output this text is
<?php
require("inc/function.inc.php");
// start authorization check
require("inc/authcheckhdr.inc.php");
$pdf = new Zend_Pdf();
// Fetch the first page so you can draw on top of it.
$page = $pdf->pages[0] = $pdf->newPage('A4');
// Set font and text color.
$page->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA), 14);
$page->setFillColor(new Zend_Pdf_Color_HTML('black'));
// Draw some text on top of the page at x,y
$page->drawText('This is a test', 72, 720);
// More drawing commands...
// Save the resulting PDF document. OR
$pdf->save('result.pdf');
// Render the resulting PDF and send it to the browser
$pdfDocument = $pdf->render();
header("Content-Type: application/pdf");
header("Content-Disposition: attachment; filename=result.pdf");
echo $pdfDocument;
?>
The pdf file is...
%PDF-1.4
%âãÏÓ
1 0 obj
<</Type /Catalog /Version /1.4 /Pages 2 0 R /Names 7 0 R >>
endobj
2 0 obj
<</Type /Pages /Kids [3 0 R ] /Count 1 >>
endobj
3 0 obj
<</Type /Page /LastModified (D:20091231114504-08'00') /Resources <</ProcSet [/Text /PDF ] /Font <</F1 8 0 R >> >> /MediaBox [0 0 595 842 ]
/Contents [4 0 R ] /Parent 2 0 R >>
endobj
4 0 obj
<</Length 50 >>
stream
/F1 14 Tf
0 g
BT
72 620 Td
(This is a test) Tj
ET
/F1 14 Tf
0 g
BT
72 720 Td
(This is a super test) Tj
ET
endstream
endobj
5 0 obj
[]
endobj
6 0 obj
<</Names 5 0 R >>
endobj
7 0 obj
<</Dests 6 0 R >>
endobj
8 0 obj
<</Type /Font /Encoding /WinAnsiEncoding /Subtype /Type1 /BaseFont /Helvetica >>
endobj
xref
0 9
0000000000 65535 f
0000000015 00000 n
0000000091 00000 n
0000000149 00000 n
0000000341 00000 n
0000000441 00000 n
0000000460 00000 n
0000000494 00000 n
0000000528 00000 n
trailer
<</ID [<65386133343365653231383339346131> <64663632306233616663343536616332> ] /Size 9 /Root 1 0 R >>
startxref
625
%%EOF