Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Office add-in PPT getFileAsync -> FileType.Pdf #5399

Open
cristrasp opened this issue Feb 18, 2025 · 8 comments
Open

Office add-in PPT getFileAsync -> FileType.Pdf #5399

cristrasp opened this issue Feb 18, 2025 · 8 comments
Assignees
Labels
Area: PowerPoint Issue related to PowerPoint add-ins Platform: Online Issues occurred on online platform

Comments

@cristrasp
Copy link

cristrasp commented Feb 18, 2025

We are using Office.js for our add-in in PowerPoint. We are trying to use getFileAsync like in this method

loadFileBlob(officeDoc: OfficeDoc): Promise<OfficeDoc> { const Office = (window as any).Office; console.log(Office.context, 'conteXT') return new Promise((resolve, reject) => { Office.context.document.getFileAsync( Office.FileType.Pdf, { sliceSize: 65536 }, (asyncResult: any) => { if (asyncResult.status === Office.AsyncResultStatus.Failed) { reject(asyncResult); } else { const file = asyncResult.value; if (file.size > 10485760) { // 10mb file.closeAsync(); asyncResult.size = file.size; reject(asyncResult); } else { this.getAllSlices(file) .then((docData: any) => { let fileContent = new String(); for (let j = 0; j < docData.length; j++) { fileContent += String.fromCharCode(docData[j]); } officeDoc.blob = this.fileUtilsService.getBlobFromBinaryString( fileContent.toString() ); resolve(officeDoc); }) .catch((err: any) => { reject(err); }); } } } ); }); }

In word and excel it works fine but in PPT even if it's written that is supported it gives a 5001 .

-Office.FileType.Pdf -> it doesn't work , but it what we need and in docs it's written that is supported.

-Office.FileType.Compressed -> it works but we don't need it .

@microsoft-github-policy-service microsoft-github-policy-service bot added the Needs: triage 🔍 New issue, needs PM on rotation to triage ASAP label Feb 18, 2025
Copy link

Here are some similar issues that might help you. Please check if they can solve your problem.

Powered by issue-sentinel

Copy link

Here are some similar issues that might help you. Please check if they can solve your problem.

Powered by issue-sentinel

@cristrasp
Copy link
Author

I tried the tutorials but it seems to give the same error

@guoms1
Copy link

guoms1 commented Feb 18, 2025

Hi, @cristrasp

Can you provide a fully runnable code snippet? Since there are no line breaks, I can’t run it directly, so I’m not sure how my code differs from yours. Also, are you running it on Desktop, Online, or Mac OS?

At least for me, the code here works fine on PowerPoint Desktop, you can import it into Script Lab and run it directly.

Best regards,

@guoms1 guoms1 added Needs: author feedback Waiting for author (creator) of Issue to provide more info Area: Common APIs Issues related to common apis Area: PowerPoint Issue related to PowerPoint add-ins and removed Needs: triage 🔍 New issue, needs PM on rotation to triage ASAP labels Feb 18, 2025
@cristrasp
Copy link
Author

cristrasp commented Feb 18, 2025

We are using online but would want to be able to use it mac and desktop too .
Since Office js is available on all of them we choose it
Our app is using angular

@microsoft-github-policy-service microsoft-github-policy-service bot added Needs: attention 👋 Waiting on Microsoft to provide feedback and removed Needs: author feedback Waiting for author (creator) of Issue to provide more info labels Feb 18, 2025
@cristrasp
Copy link
Author

cristrasp commented Feb 18, 2025

`
Office = (window as any).Office;

/**
 * Get the PowerPoint file name or return a default name.
 */
getAvailableFiles(): Promise<Array<OfficeDoc>> {
   
    const returnDoc = new OfficeDoc({});

    console.log(this.Office.context.requirements.isSetSupported("File", "1.2"), ' isSetSupported');

    return new Promise((resolve, reject) => {
        this.Office.context.document.getFilePropertiesAsync({}, async (result: any) => {
            if (typeof result.value.url !== 'undefined') {
                let fileName = 'presentation_for_export'; // Default name
                if (result.value.url !== '') {
                    fileName = result.value.url.includes('\\')
                        ? result.value.url.split('\\').pop()
                        : result.value.url.split('/').pop();
                    if (fileName.endsWith('.pptx') || fileName.endsWith('.ppt')) {
                        fileName = fileName.split('.').slice(0, -1).join('.'); // Remove extension
                    }
                }
                returnDoc.filename = fileName;
            
                console.log(returnDoc, 'returnDOC');
                resolve([returnDoc]);
            } else {
                reject('not-supported');
            }
        });
    });
}

/**
 * Export the PowerPoint presentation as a PDF.
 * Returns an OfficeDoc object with the blob populated.
 */
loadFileBlob(officeDoc: OfficeDoc): Promise<OfficeDoc> {

    return new Promise((resolve, reject) => {
        console.log('#loadFileBlob# Requesting PowerPoint file as PDF');
        this.Office.context.document.getFileAsync(this.Office.FileType.Pdf, { sliceSize: 65536 }, (asyncResult: any) => {
            if (asyncResult.status === this.Office.AsyncResultStatus.Failed) {
                console.error('#loadFileBlob# Error retrieving file:', asyncResult.error.message);
                reject(asyncResult);
            } else {
                const file = asyncResult.value;
                console.log(`#loadFileBlob# File size: ${file.size}, Slices: ${file.sliceCount}`);

                this.getAllSlices(file)
                    .then((docData: Uint8Array) => {
                        console.log('#loadFileBlob# Successfully retrieved all slices');
                        const blob = new Blob([docData], { type: 'application/pdf' });
                        officeDoc.blob = blob;
                        resolve(officeDoc);
                    })
                    .catch((err) => {
                        console.error('#loadFileBlob# Error processing slices:', err);
                        reject(err);
                    });
            }
        });
    });
}

/**
 * Get all slices of the file and combine them into a single Uint8Array.
 * @param file The file object from getFileAsync.
 */
private getAllSlices(file: any): Promise<Uint8Array> {
    return new Promise((resolve, reject) => {
        const sliceCount = file.sliceCount;
        let slicesReceived = 0;
        const docDataSlices: Uint8Array[] = [];
        let gotAllSlices = true;

        const getSlice = (index: number) => {
            file.getSliceAsync(index, (sliceResult: any) => {
                if (sliceResult.status === this.Office.AsyncResultStatus.Succeeded) {
                    console.log(`#getAllSlices# Retrieved slice ${index + 1} of ${sliceCount}`);
                    docDataSlices[sliceResult.value.index] = sliceResult.value.data;
                    slicesReceived++;

                    if (slicesReceived === sliceCount) {
                        file.closeAsync();
                        resolve(this.combineSlices(docDataSlices));
                    } else {
                        getSlice(index + 1);
                    }
                } else {
                    gotAllSlices = false;
                    file.closeAsync();
                    console.error(`#getAllSlices# Error retrieving slice ${index}:`, sliceResult.error.message);
                    reject(sliceResult.error);
                }
            });
        };

        getSlice(0);
    });
}

private combineSlices(slices: Uint8Array[]): Uint8Array {
    console.log(`#combineSlices# Combining ${slices.length} slices`);
    const combined = new Uint8Array(slices.reduce((acc, slice) => acc + slice.length, 0));
    let offset = 0;
    slices.forEach((slice) => {
        combined.set(slice, offset);
        offset += slice.length;
    });
    return combined;
}`

@cristrasp
Copy link
Author

This is the code that we are using to get the PPT in PDF format @guoms1

@guoms1
Copy link

guoms1 commented Feb 20, 2025

Hi, @cristrasp

I was able to reproduce this issue(#9821668) on PowerPoint Online. I'll be reaching out to the relevant team for further investigation.

Thanks for your patience!

Best,

@guoms1 guoms1 assigned guoms1 and unassigned EsterBergen Feb 20, 2025
@guoms1 guoms1 added Area: add-ins platform Issue related to the add-ins platform Area: PowerPoint Issue related to PowerPoint add-ins Platform: Online Issues occurred on online platform and removed Needs: attention 👋 Waiting on Microsoft to provide feedback Area: PowerPoint Issue related to PowerPoint add-ins Area: Common APIs Issues related to common apis Similar-Issue Area: add-ins platform Issue related to the add-ins platform labels Feb 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: PowerPoint Issue related to PowerPoint add-ins Platform: Online Issues occurred on online platform
Projects
None yet
Development

No branches or pull requests

3 participants