Generate PDF from base64 String PHP

I'm trying to create a .pdf file with a base64 string from an image, and I can create it correctly, but when I try to open the file, the program sends a message that tells the file is corrupt or something like that..

I got this code:

define('UPLOAD_DIR', '../image/'); $img = $_POST['image']; $img = str_replace('data:image/png;base64,', '', $img); $img = str_replace(' ', '+', $img); $data = base64_decode($img); $uniqueNumber = uniqid(); $namefile = $uniqueNumber.'.png'; $file = UPLOAD_DIR . $namefile; $success = file_put_contents($file, $data); $namefile = $uniqueNumber.'.pdf'; $file = UPLOAD_DIR . $namefile; $success = file_put_contents($file, $data);

I can open the .png file correctly so, i think it's not problem from the base64 decoded string. Thank you all!

EDIT:

I'm trying this code and getting the same issue.

$data = base64_decode ($img); //Write data back to pdf file $pdf = fopen ('test.pdf','w'); fwrite ($pdf,$data); //close output file fclose ($pdf); echo 'Done';

Is that becouse i'm saving an image with .pdf ? I think no, because if i'm doing fopen with .pdf should be with that format.

EDIT 2:

FOUND A SOLUTION.

http://www.fpdf.org/en/script/script45.php

I followed these steps and i can get that, thank you all!

-------------Problems Reply------------

Check out DOMPDF: https://github.com/dompdf/dompdf

You can definitely use DOMPDF to create a PDF with an image tag whose source is that Base64 string. and render that to PDF.

<?php
require_once("dompdf_config.inc.php");

$img = $_POST['image'];
$html =
'<html><body>'.
'<img src="'.$img.
'"></body></html>';

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");

?>

Category:php Views:15 Time:2018-12-19
Tags: php pdf corrupt

Related post

  • How to generate pdf from markdown using php? 2011-06-29

    I'm looking for a solution to make a pdf from a markdown file using PHP. I need to use custom styles as well. So i'm wondering if parsing markdown then going with webkittopdf and using custom css styles will do it. Have you used anything that can mak

  • Generate PDF from HTML PHP 2011-11-17

    I want to generate PDF from a PHP file that includes HTML controls like textbox, and textarea. I attached CSS in the same. I tried FPDF, DOMPDF and TCPDF, but still I don't get exactly what I want. How do I pass HTML controls with PHP variables and C

  • Generate PDF from ASP.NET from raw HTML/CSS content? 2009-02-20

    I'm sending emails that have invoices attached as PDFs. I'm already - elsewhere in the application - creating the invoices in an .aspx page. I'd like to use Server.Execute to return the output HTML and generate a PDF from that. Otherwise, I'd have to

  • not generating pdf from http website url 2012-01-08

    I want to generate pdf from url using servelt I am using Flying Saucer for generating pdf from website url and my code is /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package abc; import com.lowagie

  • Generate PDF from Rails 2009-01-11

    The Ruby On Rails Wiki lists a couple of libraries that facilitate PDF generation in Rails. I need to print out address labels (in letter format, thus 12-15 addresses per page) and cannot decide which one to use. Any recommendations? --------------So

  • How to generate barcode from a string using C#? 2010-01-04

    Is it possible to generate barcode from a string using c#? Is there any builtin classes for generating barcodes in asp.net? Is it possible to detect a barcode printer connected with a system? --------------Solutions------------- Yep. Of course it is

  • How to generate pdf from docbook 5.0 2010-04-10

    I've written a docbook 5.0 document with the header: <?xml version="1.0" encoding="UTF-8"?> <book version="5.0" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xi="http://www.w3.org/2001/XInclude" xmlns

  • How do you generate xml from non string data types using minidom? 2010-10-21

    How do you generate xml from non string data types using minidom? I have a feeling someone is going to tell me to generate strings before hand, but this is not what I'm after. from datetime import datetime from xml.dom.minidom import Document num = "

  • Code to generate pdf from text in iphone 2011-01-19

    Can anybody tell me Code to generate pdf from text in iphone. Thanks in advance. --------------Solutions------------- Did you try reading the documentation that outlines this? http://developer.apple.com/library/ios/#documentation/2DDrawing/Conceptual

  • Generating PDF from OpenXml 2011-09-03

    I am trying to find a SDK that can generate PDF from OpenXml. I have used the Open Xml Power Tools to convert the open XML and html and and using iTextSharp to parse the Html to PDF. But the result is a very terrible looking PDF. I have not yet tried

  • Get image dimensions from base64 string 2012-04-23

    I receive base64 string of an image and use it as source for in html page. In turn, img is a child of div. I want to get div automatically resized by the image dimensions. Tried to get dimensions of an image using javascript (... = element.style.heig

  • Modify generated json from PDO and PHP 2013-04-08

    I will be quick with more code less talk. I have a mySQL DB and i want to generate json from it with PHP and PDO. <?php require("db.php"); $dsn = "mysql:host=localhost;dbname=$dbname"; $pdo = new PDO($dsn, $username, $password); $rows = array(); $

  • Detecting image type from base64 string in PHP 2011-05-19

    Is it possible to find out the type of an image encoded as a base64 String in PHP? I have no method of accessing the original image file, just the encoded string. From what I've seen, imagecreatefromstring() can create an image resource from a string

  • Generate pdf from php(have to retrieve from mysql database) 2011-05-30

    I have already created a report using list of data from my database. i have a button in that page "Click to generate Pdf". if i click that button the same report have to come as pdf report. now i need to convert that reports as PDF. i have googled pa

  • PHP: Get image type from base64 string 2011-09-21

    How do I get the image type from a base64 string? I read another post similar to this but it doesn't give the image type, jpeg, png, etc. Below is my current code. $type = finfo_buffer(finfo_open(), base64_decode($b64Img), FILEINFO_MIME_TYPE); print(

  • Method to generate pdf from access+vb6 or just sql 2005? 2008-09-18

    The setup: Multiple computers using an adp file to access a sql 2005 database. Most don't have a pdf distiller. An access form (plain form, not crystal) is created that needs to be saved as a pdf. The only way I can think of is send a request from ac

  • Calling wkhtmltopdf to generate PDF from HTML 2009-08-26

    I'm attempting to create a PDF file from an HTML file. After looking around a little I've found: wkhtmltopdf to be perfect. I need to call this .exe from the ASP.NET server. I've attempted: Process p = new Process(); p.StartInfo.UseShellExecute = fal

  • iTextSharp to generate PDF from WPF FixedDocument 2010-08-02

    I have a simple WPF app that displays and prints some reports with a FixedDocument. How can generate PDF's from that, with a free and open solution, such as iTextSharp? --------------Solutions------------- A WPF FixedDocument, also known as an XPS do

  • Programmatically Generate PDF from HTML on iPhone 2010-10-14

    I am looking for a way to programmatically (in obj-c) generate a PDF file from a local html file. I am dynamically generating the html from user inputs, I need to create the PDF and send it to the user (via email). I am having difficulty with the PDF

  • Generating PDFs from SVG input 2011-04-29

    I am trying to generate a PDF from a SVG input file with Python in a Django application. I have already found 2 working solutions: cairo+rsvg and imagemagick but they both have one problem: They have some strange dependencies that I do not want to in

  • unique number from a string - php 2011-06-03

    I have some strings containing alpha numeric values, say asdf1234, qwerty//2345 etc.. I want to generate a specific constant number related with the string. The number should not match any number generated corresponding with other string.. ----------

  • Create new PDF from template with PHP 2011-06-22

    I have a docx document file set up with formating and layout, which I saved as a PDF file. I want to use this pdf file as a template for creating new pdf files pre-filled with user data. I have seen FPDF, FPDI, tcPDF, domPDF, Zend PDF, http://koivi.c

  • flying saucer / jruby on rails generate pdf from view 2011-07-18

    I am trying to generate a pdf from a view in jruby on rails using flying saucer with the following code in the controller: def calendar @patient = Patient.find_by_id(params[:id]) result = render_to_string send_data( FlyingSaucer::create_pdf(result),

  • Convert from base64 string to long in C# 2012-02-08

    Hi I have some strings generated using the following code: private static string CalcHashCode(byte[] data) { MD5CryptoServiceProvider md5Provider = new MD5CryptoServiceProvider(); Byte[] hash = md5Provider.ComputeHash(data); return Convert.ToBase64St

  • Generate PDF from a view but content is not in English 2013-07-01

    I'm trying to generate a PDF doc from mvc4 view but none English characters do not display or display as gibberish(for English it works fine). I tried to use pdfRazor and itext but only English chars are shown on screen. I'm open to any solution even

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.149 (s). 11 q(s)