我使用fabric.js绘制一些明信片(背景,图像,文字).我想基于这张卡创建pdf.所以我将json对象发送到php并使用foreach来获取所有元素.但是当我在同一分辨率(761x430)上创建文档并从json设置边距,位置和其他值时,pdf中的对象位置与结构js中的对象位置不同.所以我应该做的是,我的pdf中的位置与画布上的位置相同.要创建pdf我使用TCPDF.
JSON文件
{"objects":[{"type":"text","originX":"center","originY":"center","left":531.22,"top":249,"width":115,"height":31.2,"fill":"e2ddcf","stroke":null,"strokeWidth":5,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,"scaleX":1.14,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","text":"MOJE T?O","fontSize":24,"fontWeight":"normal","fontFamily":"Times New Roman","fontStyle":"normal","lineHeight":1.3,"textDecoration":"","textAlign":"left","path":null,"textBackgroundColor":"","useNative":true},{"type":"image","originX":"center","originY":"center","left":63,"top":304,"width":66,"height":66,"fill":"rgb(0,0,0)","stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,"scaleX":1,"scaleY":1,"angle":340.06,"flipX":false,"flipY":false,"opacity":1,"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","src":"src_to_picture","filters":[]}],"background":"cyan","backgroundImage":"src_to_my_picture","backgroundImageOpacity":1,"backgroundImageStretch":true}
PHP代码
$pdf = new MYPDF("L", "px", array(761, 430), true, 'UTF-8', false, false, $objects->backgroundImage, $objects->background); $pdf->SetCreator(PDF_CREATOR); $pdf->SetLeftMargin(0); $pdf->SetRightMargin(0); $pdf->SetHeaderMargin(0); $pdf->SetFooterMargin(0); $pdf->setPrintFooter(false); #$pdf->setPrintHeader(false); $pdf->AddPage(); foreach ($objects->objects as $object) { $pdf->StartTransform(); switch ($object->type) { case 'text': $align = $this->setAlign($object->textAlign); $style = $this->setStyle($object->fontStyle); $newColor = $this->hex2RGB($object->fill); $pdf->setXY($object->left, $object->top); $pdf->SetFont("times", $style, $object->fontSize); $pdf->SetTextColor($newColor['red'], $newColor['green'], $newColor['blue']); $pdf->MultiCell(0, $object->height, $object->text, 0, $align, false, 1, '', '', true, 0, false, true, 0, 'T', false); break; case 'image': $pdf->setXY($object->left, $object->top); $pdf->Rotate(360-$object->angle); $pdf->Image($object->src, $object->left, $object->top, $object->width, $object->height, '', '', '', false, 300, '', false, false, 0); break; default: break; } $pdf->StopTransform(); } $pdf->Close(); echo $pdf->Output('test.pdf', 'D');
vuz3.. 5
好吧它工作,很容易使这个代码工作:D左和上是从对象的中心给出,所以足够的左边距使用减去对象宽度的一半,它的工作原理
$pdf = new MYPDF("L", "px", array(761, 430), true, 'UTF-8', false, false, $objects->backgroundImage, $objects->background); $pdf->SetCreator(PDF_CREATOR); $pdf->SetLeftMargin(0); $pdf->SetRightMargin(0); $pdf->SetHeaderMargin(0); $pdf->SetFooterMargin(0); $pdf->setPrintFooter(false); #$pdf->setPrintHeader(false); $pdf->AddPage(); foreach ($objects->objects as $object) { $pdf->StartTransform(); $left = $object->left - ($object->width/2); $top = $object->top - ($object->height/2); switch ($object->type) { case 'text': $align = $this->setAlign($object->textAlign); $style = $this->setStyle($object->fontStyle); $newColor = $this->hex2RGB($object->fill); $pdf->setXY($left, $top); $pdf->SetFont("times", $style, $object->fontSize); $pdf->SetTextColor($newColor['red'], $newColor['green'], $newColor['blue']); $pdf->MultiCell(0, $object->height, $object->text, 0, $align, false, 1, '', '', true, 0, false, true, 0, 'T', false); break; case 'image': $pdf->setXY($left, $top); $pdf->Rotate(360-$object->angle); $pdf->Image($object->src, $object->left, $object->top, $object->width, $object->height, '', '', '', false, 300, '', false, false, 0); break; default: break; } $pdf->StopTransform(); } $pdf->Close(); echo $pdf->Output('test.pdf', 'D');
如果有人对此有问题,以上是正确的代码.
好吧它工作,很容易使这个代码工作:D左和上是从对象的中心给出,所以足够的左边距使用减去对象宽度的一半,它的工作原理
$pdf = new MYPDF("L", "px", array(761, 430), true, 'UTF-8', false, false, $objects->backgroundImage, $objects->background); $pdf->SetCreator(PDF_CREATOR); $pdf->SetLeftMargin(0); $pdf->SetRightMargin(0); $pdf->SetHeaderMargin(0); $pdf->SetFooterMargin(0); $pdf->setPrintFooter(false); #$pdf->setPrintHeader(false); $pdf->AddPage(); foreach ($objects->objects as $object) { $pdf->StartTransform(); $left = $object->left - ($object->width/2); $top = $object->top - ($object->height/2); switch ($object->type) { case 'text': $align = $this->setAlign($object->textAlign); $style = $this->setStyle($object->fontStyle); $newColor = $this->hex2RGB($object->fill); $pdf->setXY($left, $top); $pdf->SetFont("times", $style, $object->fontSize); $pdf->SetTextColor($newColor['red'], $newColor['green'], $newColor['blue']); $pdf->MultiCell(0, $object->height, $object->text, 0, $align, false, 1, '', '', true, 0, false, true, 0, 'T', false); break; case 'image': $pdf->setXY($left, $top); $pdf->Rotate(360-$object->angle); $pdf->Image($object->src, $object->left, $object->top, $object->width, $object->height, '', '', '', false, 300, '', false, false, 0); break; default: break; } $pdf->StopTransform(); } $pdf->Close(); echo $pdf->Output('test.pdf', 'D');
如果有人对此有问题,以上是正确的代码.