好的,我已经想好了怎么做,好像有点乱,可惜没有更简单的方法!
我使用的两个插件是 cforms (www.deliciousdays.com/cforms-plugin) 和一个加载 Zend 框架的插件 (h6e.net/wordpress/plugins/zend-framework) 这是需要它的 pdf 写作能力。
激活两个插件后,在 cforms 插件目录中找到名为 my-functions.php 的文件并下载到您的计算机。这个文件的大部分都被注释掉了,所以你需要取消注释函数
function my_cforms_action($cformsdata) {
}
当提交按钮被按下时,这个函数中的任何东西都会运行(有关更多详细信息,请参阅 cforms API 文档)。您可以测试它是否有效,但会在 this 函数中回显某些内容。您还可以使用测试 Zend 框架是否已加载
if (defined('WP_ZEND_FRAMEWORK') && constant('WP_ZEND_FRAMEWORK')) {
echo 'Zend is working!';
}
表单字段以数组形式出现,因此我们需要先将其打印出来以计算字段名称(将“5”替换为您使用的任何表单):
$formID = $cformsdata['id'];
$form = $cformsdata['data'];
if ( $formID == '5' ) {
print_r($form);
}
获得字段名称后,这是写入 pdf 的完整代码
//Get the ID of the current form and all the data that's been submitted
$formID = $cformsdata['id'];
$form = $cformsdata['data'];
//run this code only if it's the form with this ID
if ( $formID == '5' ) {
//Loads the Zend pdf code
require_once 'Zend/Pdf.php';
//Set the path of the pdf (in the root of my wordpress installation)
$fileName = 'c100-eng2.pdf';
//loads the pdf
$pdf = Zend_Pdf::load($fileName);
//Selects the page to write to
$page = $pdf->pages[0];
//Selects which font to use
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
$page->setFont($font, 12);
//Writes the text from the field 'Your name' 210 points from the left and 420 points from the bottom of the selected page
$page->drawText($form['cf_form5_Your name'], 210, 420);
//for some reason there is no way to wrap text using Zend_pdf so we have to do it ourselves for any paragraphs...
//starting 600 points from the bottom of the page
$startPos = 600;
//we're using the form field "About you"
$paragraph = $form['cf_form5_About you'];
//sets the width of the paragraph to 30 characters
$paragraph = wordwrap( $paragraph, 30, '\n');
//breaks paragraph into lines
$paragraphArray = explode('\n', $paragraph);
//writes out the lines starting 200 points from the left with a line height of 12 points
foreach ($paragraphArray as $line) {
$line = ltrim($line);
$page->drawText($line, 200, $startPos);
$startPos = $startPos - 12;
}
//saves the pdf
$pdf->save('new.pdf');
我一开始就遇到了问题,因为我使用的 pdf 类型不正确,您可以通过使用 Zend pdf 创建一个 pdf 来检查您的代码是否正确,因为这应该始终有效(在此处了解如何执行此操作:framework.zend.com/manual/1.12/en/zend.pdf.html)。
我使用 Photoshop 通过将标尺设置为“点”来计算出我想要书写的确切位置