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 Overflowruby on rails - Convert base64 pdf string into an image - Stack Overflow
How to convert PDF Base64 into pdf then PDF into an image in C# - Stack Overflow
Convert Base64 Encoded PDF to Base64 Encoded PNG
How can I convert Base64 to JPG or PDF in the cloud?
What is the file size limit of PDF to Base64 converter?
Can I later re-convert the Base64 code back to a PDF document?
How does this tool handle large PDF files during Base64 encoding—will it split or compress the data?
In Imagemagick, you can do that with the INLINE: input structure. See http://www.imagemagick.org/Usage/files/#inline. For example:
convert 'inline:data:image/png;base64,
R0lGODlhIAAgAPIEAAAAAB6Q/76+vvXes////wAAAAAAAAAAACH5BAEAAAUALAAA
AAAgACAAAAOBWLrc/jDKCYG1NBcwegeaxHkeGD4j+Z1OWl4Yu6mAYAu1ebpwL/OE
YCDA0YWAQuJqRwsSeEyaRTUwTlxUqjUymmZpmeI3u62Mv+XWmUzBrpeit7YtB1/r
pTAefv942UcXVX9+MjNVfheGCl18i4ddjwwpPjEslFKDUWeRGj2fnw0JADs=
' b64_noseguy.png

file1 = Tempfile.new(['order', '.pdf'])
File.open(file1.path.to_s, 'wb') do |f|
f.write(Base64.decode64(label_string))
end
file = Tempfile.new(['order', '.png'])
im = Magick::Image.from_blob(open(file1.path.to_s).read).first
im.write(file.path.to_s)
pdf = Services::Invoices::Pdf.new(invoice).process(file.path)
file.unlink
pdf
This works, however; it does not seem like it would be the best solution.
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.
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.
I'm currently creating a Flutter Frontend that gets a Base64 Encoded PDF File. This information is sent to a C# API that will convert the base64 PDF to base64 PNG(or any other image format) which can then be displayed on the frontend.
Now after hours of research I cannot find a solution that doesn't involve a paid for package in C#
So any advice or even a point in the right direction would be greatly appreciated.
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?
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:
- Strip
data:image/png;base64,from the URI usingexplode - Decode the URI with
base64_decode - Save the decoded data to a PNG file with
file_put_contents - Generate a PDF and add the PNG file with
Image - 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);
}
}
I know this is an old topic, but there is an easier way of solving this problem. Just try this simple steps:
- Strip data:image/png;base64 from URI using explode
- Concatenate
data://text/plain;base64,with what is left from the stripped URI - 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.