2

谁能告诉我如何编写一个简单的 C++ 代码来将我的数据(从变量)导出到 PDF 文件而不使用任何外部库或实用程序?

4

5 回答 5

6

使用fprintf和写入文件,符合PDF 文件格式

请特别查看附录 G。您可能能够摆脱一些非常简单的转换并生成可读的 PDF 文件。

于 2009-08-05T04:26:25.917 回答
2

如果不使用 PDF 库,您最好的方法可能是使用其他应用程序(例如 OpenOffice)创建带有示例文本的简单 PDF。然后将其用作样板的基础,并让您的 C++ 代码在适当位置插入您自己的文本。请注意,PDF 是面向行的,因此每行新的文本都必须明确放置在正确的位置。

对于除了非常简单的输出之外的任何其他内容,如果不完全理解 PDF 规范,这将很棘手

于 2009-08-05T04:30:27.827 回答
2

谁能告诉我如何编写一个简单的 C++ 代码来将我的数据(从变量)导出到 PDF 文件而不使用任何外部库或实用程序?

不,PDF 文件很复杂。要读写它们,你的代码会很复杂。通常,您可以使用外部库来隐藏这种复杂性,但如果您不想这样做,那么您的代码将不得不包含复杂性。然后就不会简单了。

你要求的是不可能的。您要求执行一项复杂的任务,而不是自己做任何复杂的事情,也不依赖其他任何人为您做复杂的事情。但有人将不得不这样做。

于 2009-08-05T10:44:13.580 回答
1

我目前正在使用 ZendPDF,并且我创建了一个测试 pdf 文件,当作为文本查看时,它看起来像这样。当我在 pdf 文件本身中将文本“这是一个测试”更改为“这是一个超级测试”时,我能够更改最终的 pdf 文件并在 foxit 中打开它。因此,复制它并使用 C++ 将字符串替换为您想要的。通过复制 F1 14 Tf 的内容,我还能够仅使用文本编辑器使新文本出现。

用于输出此文本的 php 是

<?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; 

?>

pdf文件是...

%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
于 2009-12-31T19:52:26.207 回答
0

...不使用任何外部库或实用程序?

这有点问题...如果您使用的是QT,您可以做到...您的设置是什么?

于 2009-08-05T04:16:37.323 回答