Error: Cannot Read Properties of Undefined (Reading ‘readBytesFromStream’)

The first way gives an error saying Cannot read properties of undefined (reading ‘readBytesFromStream’) can you please let me know how this can be resolved?

I don’t want to save the image in local server before creating a slide with image.
aspose_readbytesFromStream_error.png (7.0 KB)

@arun08,
Could you please share a complete code example and files to reproduce the error on our end?

This is the code block which is used

// presentation: const presentation = new aspose.Presentation(payload.templateFile); instance
// placeholder object 
function fillImageBase64PlaceHolder(
  shape: any,
  placeHolder: IPlaceHolder,
  presentation: any
) {
  // placeholder.data is the image data which has base64
  var base64Image = placeHolder.data;
  const [, encodedData] = base64Image.split(',');
  const imageBytes = Buffer.from(encodedData, 'base64');
  const imageStream = new Readable();
  imageStream.push(imageBytes);
  imageStream.push(null);
  console.log(imageBytes);
  aspose.slides.readBytesFromStream(imageStream, function (imgArray: any) {
    console.log("inside base64 image");
    var img = presentation.getImages().addImage(imgArray);
    console.log(img);
    shape.getFillFormat().setFillType(java.newByte(aspose.FillType.Picture));
    shape.getFillFormat().getPictureFillFormat().setPictureFillMode(aspose.PictureFillMode.Stretch);
    shape.getFillFormat().getPictureFillFormat().getPicture().setImage(img);
  })
  imageStream?.close();
}

@arun08,
Thank you for the code example. Could you please also share a code that calls the function fillImageBase64PlaceHolder and presentation file you used?

function fillPlaceHolderAsPerType(
  shape: any,
  placeHolder: IPlaceHolder,
  presentation: any
) {
  switch (placeHolder.type) {
    case PlaceHolderType.Text:
      fillTextPlaceHolder(shape, placeHolder);
      break;
    case PlaceHolderType.Doughnut:
      fillDoughnutPlaceHolder(shape, placeHolder);
      break;
    case PlaceHolderType.Line:
    case PlaceHolderType.LineWithMarker:
      fillLinePlaceHolder(shape, placeHolder);
      break;
   
    case PlaceHolderType.Map:
      fillMapPlaceHolder(shape, placeHolder);
      break;
    case PlaceHolderType.ImageBase64:
      fillImageBase64PlaceHolder(shape, placeHolder, presentation);
      break;
    default:
      console.log("Placeholder not matched", placeHolder);
  }
}

unable to upload the pptx file here
new_template_file.zip (203.9 KB)

This is something which I want to implement, because I don’t want to store multiple images in my server which will cause overhead in storage and retrieval. Kindly do the needful. Even if you have a sample code (complete code) which can accommodate this will be good.

@arun08,
Thank you for the details. I am working on the issue and will get back to you soon.

thank you for taking this on priority.

@arun08,
Thank you for your patience. Unfortunately, I was unable to reproduce the error you described. Could you please check the error using the latest version of Aspose.Slides for Node.js?

Can you share your code sample which i can take reference and check ??

That way i can run that in local and make the final decision

@arun08,
I used the first code example provided above.

@andrey.potapov , but it does not have any import statement and the calling function etc. which can give me insights on what i am missing. Can you share the entire stuff ? Since i am unable to do it in my local ?

Thanks in advance.

@arun08,
Please try using the following code example:

var aspose = aspose || {};

const java = require("java");
const { Readable } = require("stream");
const asposeSlides = require("aspose.slides.via.java");

const base64Image = "data:image/png;base64,..." // use valid data here

var presentation = new asposeSlides.Presentation();

const [, encodedData] = base64Image.split(',');
const imageBytes = Buffer.from(encodedData, 'base64');
const imageStream = new Readable();
imageStream.push(imageBytes);
imageStream.push(null);
asposeSlides.readBytesFromStream(imageStream, function (imgArray) {
    // ...
});

presentation.dispose();

thanks, will try this and get back.

@arun08,
Thanks, we will be waiting for your feedback.

@andrey.potapov thanks for the help, I was able to add the image in the new presentation but it displayed only half of the real image also i have a template where i have an sample image i am trying to replace or add the base64 image in that place, is it possible to do ?

@arun08,
It looks like you have two issues.

Please share the following files:

  • sample presentation file
  • input image file
  • output presentation file

Then we will check the problem and help you.

I am working on the question and will get back to you soon.

TemplateFile.zip (406.0 KB)

I have added the sample image file, the requirement is to add base64 image data into the slides “imageplaceholder” , have added the base64image data along with this folder.
Kindly let me know for more details

@arun08,
Thank you for the details. I will get back to you soon.

@arun08,
The dimensions of the image (base64 data) and the dimensions of the “imageplaceholder” are different. Could you please specify the expected result?