Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

php - TCPDF and insert an image base64 encoded

I've this string from Mysql db:

$img_base64_encoded = 

'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+PCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkIj48c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmVyc2lvbj0iMS4xIiB3aWR0aD0iMzgiIGhlaWdodD0iNTIiPjxwYXRoIHN0cm9rZS1saW5lam9pbj0icm91bmQiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIgc3Ryb2tlLXdpZHRoPSIyIiBzdHJva2U9InJnYigxMjEsIDEyMSwgMTIxKSIgZmlsbD0ibm9uZSIgZD0iTSA5IDggYyAtMC4wMyAwLjM1IC0xLjE5IDEzLjIzIC0yIDIwIGMgLTAuMjEgMS43MiAtMC40MSAzLjQ4IC0xIDUgYyAtMS4wMyAyLjY3IC0zLjI2IDUuMzMgLTQgOCBjIC0wLjgzIDMgLTEgMTAgLTEgMTAiLz48cGF0aCBzdHJva2UtbGluZWpvaW49InJvdW5kIiBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlPSJyZ2IoMTIxLCAxMjEsIDEyMSkiIGZpbGw9Im5vbmUiIGQ9Ik0gOSAyOSBsIDExIC01Ii8+PHBhdGggc3Ryb2tlLWxpbmVqb2luPSJyb3VuZCIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2Utd2lkdGg9IjIiIHN0cm9rZT0icmdiKDEyMSwgMTIxLCAxMjEpIiBmaWxsPSJub25lIiBkPSJNIDIxIDYgYyAwIDAuNjEgMC41MiAyNS4wNiAwIDM1IGMgLTAuMDUgMS4wMiAtMiAzIC0yIDMiLz48cGF0aCBzdHJva2UtbGluZWpvaW49InJvdW5kIiBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlPSJyZ2IoMTIxLCAxMjEsIDEyMSkiIGZpbGw9Im5vbmUiIGQ9Ik0gMzUgMSBjIDAuMDQgMC4wNSAxLjg5IDEuOTggMiAzIGMgMC40OCA0LjUzIDAuNzUgMTEuMTQgMCAxNiBjIC0wLjUgMy4yNyAtMy4zOCA2Ljc1IC00IDEwIGMgLTAuNjMgMy4zMiAwIDExIDAgMTEiLz48L3N2Zz4='

It show "HI" in a image. Now i want to embed this image encoded directly inside a PDF generated with TCPDF plugin but always get error:

TCPDF ERROR: [Image] Unable to get the size of the image: 

And this is how to insert it:

$img = '<img src="' . $img_base64_encoded . '">';
$pdf->writeHTML($img, true, false, true, false, '');

I've also tried with:

  $pdf->Image('@' . $img_base64_encoded);

with the same error.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well, you can actually, looking at the code you just neet to add a '@' before the base64 encoded string:

$img_base64_encoded = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA0gA...';

$img = '<img src="@' . preg_replace('#^data:image/[^;]+;base64,#', '', $img_base64_encoded) . '">';

$pdf->writeHTML($img, true, false, true, false, '');

Tested with the last version of TCPDF


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

1.4m articles

1.4m replys

5 comments

57.0k users

...