You can try making an Image object. Then in the src put the base64.

var myImage = new Image();
myImage.src="data:image/png;base64,"+file; 
Answer from Saptarshi Dey on Stack Overflow
🌐
Base64.Guru
base64.guru › converter › decode › image
Base64 to Image | Base64 Decode | Base64 Converter | Base64
Convert Base64 to image online using a free decoding tool which allows you to decode Base64 as image and preview it directly in the browser. In addition, you will receive some basic information about this image (resolution, MIME type, extension, size). And, of course, you will have a special link to download the image to your device.
Discussions

ruby on rails - Convert base64 pdf string into an image - Stack Overflow
I still continue to receive a blank image when I render it. I need to generate a png file from this base64 string. The label_string is the base64 encoded pdf that I receive from a shipping label creater. More on stackoverflow.com
🌐 stackoverflow.com
How to convert PDF Base64 into pdf then PDF into an image in C# - Stack Overflow
Just a thought, you don't need to create a physical PDF file, you can have it in memory and convert it to image from there. More on stackoverflow.com
🌐 stackoverflow.com
Convert Base64 Encoded PDF to Base64 Encoded PNG
Is this for a paid project (i.e for your work?) if so, then I'd recommend using a paid library like iText or Aspose. iText is also licensed under the AGPL, so if you're project is compatible with that then go ahead and use that. Otherwise, you can render a PDF to an image using ImageMagick and there exists a .NET wrapper for it, I've used it before. There is more information and other ways to do it here More on reddit.com
🌐 r/csharp
9
3
February 9, 2024
How can I convert Base64 to JPG or PDF in the cloud?
Solved it myself. Compose Base64tobinary then save that to sharepoint with a .pdf at the end More on reddit.com
🌐 r/MicrosoftFlow
6
5
August 31, 2023
People also ask

What is the file size limit of PDF to Base64 converter?
There are no file size restrictions on any of iLovePDF2’s converters. If needed, you can reduce PDF size beforehand using the Compress PDF tool.
🌐
ilovepdf2.com
ilovepdf2.com › convert-pdf-to-base64
Convert PDF To BASE64 Image Code - I Love PDF 2
Can I later re-convert the Base64 code back to a PDF document?
Yes, you can easily re-convert the Base64 code back into a PDF document using our Base64 to PDF converter.
🌐
ilovepdf2.com
ilovepdf2.com › convert-pdf-to-base64
Convert PDF To BASE64 Image Code - I Love PDF 2
How does this tool handle large PDF files during Base64 encoding—will it split or compress the data?
The converter encodes each page individually without compressing or splitting the data, maintaining full quality and integrity while keeping each page's encoding separate.
🌐
ilovepdf2.com
ilovepdf2.com › convert-pdf-to-base64
Convert PDF To BASE64 Image Code - I Love PDF 2
Top answer
1 of 2
3

So this is your current code:

byte[] PDFDecoded = Convert.FromBase64String(base64BinaryStr);
string FileName = (@"C:\Users\Downloads\PDF " + DateTime.Now.ToString("dd-MM-yyyy-hh-mm"));

BinaryWriter writer = new BinaryWriter(File.Create(FileName + ".pdf"));            
writer.Write(PDFDecoded);

You don't actually need BinaryWriter for this. File.Create already gives you a FileStream:

FileStream writer = File.Create(FileName + ".pdf");
writer.Write(PDFDecoded, 0, PDFDecoded.Length);

But this will still have the problem you're experiencing because you're not flushing the data to it. We also need to close the file. Thankfully, we can wrap it in using and it will do both for us:

using (FileStream writer = File.Create(FileName + ".pdf"))
{
    writer.Write(PDFDecoded, 0, PDFDecoded.Length); 
}

But a simpler way to do this is:

File.WriteAllBytes(FileName + ".pdf", PDFDecoded);

As for PDF -> Image, you'll probably have to see if there is a library available for this (search "PDF to Image NuGet") that can help you with this as I don't think there is anything built-in.

2 of 2
2

Just a thought, you don't need to create a physical PDF file, you can have it in memory and convert it to image from there.

Now the problem is that you cannot use Image from System.Drawing.Imaging for this, it doesn't support reading PDF file.

Instead you'll need to search for some library that can do that.
For example, try GemBox.Pdf, you can use it like this:

string base64String = "...";
byte[] pdfBytes = Convert.FromBase64String(base64String);

using (PdfDocument pdfDocument = PdfDocument.Load(new MemoryStream(pdfBytes)))
{
    ImageSaveOptions imageOptions = new ImageSaveOptions(ImageSaveFormat.Png);

    string imageName = DateTime.Now.ToString("dd-MM-yyyy-hh-mm") + ".png";
    pdfDocument.Save(@"C:\Users\Downloads\" + imageName, imageOptions);
}

I've used the code provided on this Convert example.

🌐
Base64.Guru
base64.guru › home › base64 converter › base64 encode
PDF to Base64 | Base64 Encode | Base64 Converter | Base64
It automatically detects the content type of the uploaded PDF file, so that you simply copy the complete result. If you do not know what output format you need, check the following examples to see how will look the result of the same Base64-encoded PDF file formatted in each of the available formats (as an example Base64 string I use first 64 bytes of a PDF file):• Plain text:
Find elsewhere
🌐
Craftmypdf
support.craftmypdf.com › using a base64 image in a pdf
Using a base64 image in a PDF | CraftMyPDF Documentation
Embed base64-encoded images in CraftMyPDF templates using data URLs and expressions in the image Src property.
🌐
ILovePDF 2
ilovepdf2.com › convert-pdf-to-base64
Convert PDF To BASE64 Image Code - I Love PDF 2
August 1, 2025 - Step 2: Simply hit the Blue ‘Convert to Base64’ button to begin encoding your PDF file. Wait until the conversion process is completed · Step 3: Now a green download button will appear, select it to receive a zip file containing .txt files. The newly converted file holds the Base64 image code for all the individual pages of your PDF document.
🌐
Reddit
reddit.com › r/microsoftflow › how can i convert base64 to jpg or pdf in the cloud?
r/MicrosoftFlow on Reddit: How can I convert Base64 to JPG or PDF in the cloud?
August 31, 2023 -

Trying to convert a Base64 string to JPG or PDF in Power Automate in the cloud.

Basic flow is creating return shipment labels via API. It is returned as Base64 and i'd like to convert it to a JPG or PDF in the cloud. Is it possible?

🌐
LEADTOOLS
leadtools.com › blog › document-imaging › document-converter › convert-base64-encode-images-pdfs
Convert and Base64 Encode Images and PDFs in C# | LEADTOOLS Blog
July 3, 2021 - byte[] bytes; using (var codecs ... codecs.Save(e.Image, memStream, RasterImageFormat.RasPdfJpeg, 24); bytes = memStream.ToArray(); } var base64 = Convert.ToBase64String(bytes);...
🌐
IPVoid
ipvoid.com › base64-to-image
Base64 to Image Online, Convert Base64 to Image | IPVoid
Use this online base64 to image tool to convert a base64-encoded image into any image format (i.e JPG, PNG, ICO, WEBP, AVIF), so you can preview it in your browser and download it as image file in your device. The simplest way to decode base64 as image online.
Top answer
1 of 8
30

While the comments suggested a TCPDF solution, I did want to state that there is a way to do this entirely with FPDF. The thought process is like this:

  1. Strip data:image/png;base64, from the URI using explode
  2. Decode the URI with base64_decode
  3. Save the decoded data to a PNG file with file_put_contents
  4. Generate a PDF and add the PNG file with Image
  5. Delete the PNG file with unlink

Always remember to error check. If the image fails to save to the server you obviously do not want to continue execution. Make sure to always strictly check if the functions return false!

Here is my solution. I used a tiny image for a small URI in this example.

const TEMPIMGLOC = 'tempimg.png';

$dataURI    = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAPCAMAAADarb8dAAAABlBMVEUAAADtHCTeKUOwAAAAF0lEQVR4AWOgAWBE4zISkMbDZQRyaQkABl4ADHmgWUYAAAAASUVORK5CYII=";
$dataPieces = explode(',',$dataURI);
$encodedImg = $dataPieces[1];
$decodedImg = base64_decode($encodedImg);

//  Check if image was properly decoded
if( $decodedImg!==false )
{
    //  Save image to a temporary location
    if( file_put_contents(TEMPIMGLOC,$decodedImg)!==false )
    {
        //  Open new PDF document and print image
        $pdf = new FPDF();
        $pdf->AddPage();
        $pdf->Image(TEMPIMGLOC);
        $pdf->Output();

        //  Delete image from server
        unlink(TEMPIMGLOC);
    }
}
2 of 8
24

I know this is an old topic, but there is an easier way of solving this problem. Just try this simple steps:

  1. Strip data:image/png;base64 from URI using explode
  2. Concatenate data://text/plain;base64, with what is left from the stripped URI
  3. Use the result string(base64) as argument of FPDF Image()

Example:

$dataURI = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAPCAMAAADarb8dAAAABlBMVEUAAADtHCTeKUOwAAAAF0lEQVR4AWOgAWBE4zISkMbDZQRyaQkABl4ADHmgWUYAAAAASUVORK5CYII=";

$img = explode(',',$dataURI,2)[1];
$pic = 'data://text/plain;base64,'. $img;

$pdf = new FPDF();
$pdf->AddPage();
$pdf->Image($pic, 10,30,0,0,'png');
$pdf->Output();

Remember to choose the correct image type for Image() method call.

🌐
Base64
base64.dev › home › base64 to image
Base64 to Image Converter (Decode & Download) — base64.dev
That's fine here — the tool ... of a text file, a PDF, or some other blob will decode into bytes, but the browser can't render it as an image....
🌐
Stack Overflow
stackoverflow.com › questions › 65725309 › javascript-how-to-convert-and-download-base64-pdf-data-as-an-png-image
Javascript - How to convert and download base64 pdf data as an png image? - Stack Overflow
You basically need to do two things ... a PNG image: ... One way to do this is through using something like the LEADTOOLS Document Library (which is what I am familiar with since I work for the vendor of this toolkit). Using this library, you can be implement the DocumentConverter class in a server-side C# code that decodes the base64 string, converts PDF to PNG, then ...
🌐
Tooljet
docs.tooljet.com › upload and view images and pdfs using base64 string
Upload And View Images and PDFs Using Base64 String | ToolJet
Under the Columns section, add two columns - pdf and image. ... In the above query, we are using the exposed variables of both Filepicker components to get the base64 strings of the files we had uploaded earlier.
🌐
Base64.Guru
base64.guru › home › base64 converter › base64 decode › image
Base64 to JPG | Image | Base64 Decode | Base64 Converter | Base64
Convert Base64 to JPG online using a free decoding tool that allows you to decode Base64 as JPG image and preview it directly in the browser.
🌐
Base64 Encode
base64encode.net › pdf-to-base64
PDF to Base64 Encode - Online Tool
Only PDFs with the following formats (mime types) can be encoded using this Base64 PDF Encoder tool: application/pdf · application/x-pdf · Image to Base64 · File to Base64 · PDF to Base64 · Base32 Encode ·
🌐
Aspose
products.aspose.app › pdf apps › convert › pdf to base64
PDF to BASE64 online — PDF to base 64
4 weeks ago - PDF to Base64 Converter is a service for online file conversion from one type to another. We support many popular formats for work, all possible image formats, multimedia file formats, etc.
🌐
SmallPDFfree
smallpdffree.com › home › pdf to base64
PDF To Base64 - SmallPDFfree
September 1, 2024 - Step 02: Click on “Convert to Base64” button to start converting PDF to Base64. Step 03: Once download button appeared – click on it to start downloading a zip file. Inside that zip file, you will find .txt files containing the base64 image code for each PDF page separately.