Download converted pdf file

I need to download this file

 // Convert HTML file to PDF
var convertResponse = pdfApi.PutHtmlInStorageToPdf(name: manifest.Output.OutputFileName, srcPath: name, height: height, width: width, marginLeft: left_margin, marginBottom: bottom_margin, marginRight: right_margin, marginTop: top_margin);

// Download a File from Storage
var downloadResponse = pdfApi.DownloadFile("C:\Home\site\wwwroot", manifest.Output.OutputFileName);


I am getting this error

Aspose.Pdf.Cloud.Sdk.Client.ApiException: 'Error calling DownloadFile: {"RequestId":"de04b2da-d256-41ba-af92-fcad41814cxx","Error":{"Code":"errorItemNotFound","Message":"The storage TeamTemplate.pdf was not found or is not associated with the application.","Description":"Operation Failed. Item Not Found.","DateTime":"2023-12-15T07:16:53.9550856Z","InnerError":null}}'
'
1 Like

@niku2023

Please double check the file path in cloud storage and use the DownloadFile method as following.

// Download file Cloud storage
var downloadResponse = pdfApi.DownloadFile(cloudFolder + "/" + cloudFileName);
var fileStream = System.IO.File.Create("C:/Temp/Test.pdf");
downloadResponse.CopyTo(fileStream);
fileStream.Close();

Now i am getting this error.

Aspose.Pdf.Cloud.Sdk.Client.ApiException: ‘Error calling DownloadFile: {“RequestId”:“756ddf36-db56-4177-xxeb-538cbf1cb6xx”,“Error”:{“Code”:“errorWindowsAzureStorage”,“Message”:“Windows Azure Storage exception: The remote server returned an error: (404) Not Found.”,“Description”:“Operation Failed. Windows Azure Storage Error.”,“DateTime”:“2023-12-15T13:18:49.34683xx”,“InnerError”:null}}’

I think i have spoiled my code. I have my html file in azure input container. and i need to convert it into a pdf and need to upload it in to the azure output container again. sorry for sending same question again and again. please help to fix this

@niku2023

Please share a sample working code project with us for investigation. We will look into it and guide you accordingly.

This is my full Code.

    async Task<byte[]> ITransformHelper.ConvertHtmlToPdf(string htmlDocument, Manifest manifest, string header, string footer, string requestId)
    { 
        string name = "Team3WorkOrderTemplate.html"; 
        int height = 850;
        int width = 600;
        int top_margin = 10;
        int bottom_margin = 10;
        int right_margin = 10;
        int left_margin = 10;
        string docFilePath = "";
        string htmlHeaderPath = "";
        string htmlFooterPath = "";

        string currentPath = Directory.GetCurrentDirectory();
        string parentPath = Directory.GetDirectoryRoot(currentPath);
        string filePath = Path.Combine(parentPath, @"home\site\wwwroot\");
        string htmlFilePath = "";

        var pdfApi = new PdfApi("6bbdf10db4190256c30982673947b15f", "5ede2462-5197-402d-ae34-aa954d807171");

        if (!string.IsNullOrEmpty(manifest.Output.OutputFileName))
        {
            htmlFilePath = Path.Combine(filePath, manifest.Output.OutputFileName?.Replace(".pdf", ".html"));
            docFilePath = Path.Combine(filePath, manifest.Output.OutputFileName);
            File.WriteAllText(htmlFilePath, htmlDocument);

          
            using (var file = System.IO.File.OpenRead(htmlFilePath))
            {
                // Convert HTML file to PDF
                var convertResponse = pdfApi.PutHtmlInStorageToPdf(name: manifest.Output.OutputFileName, srcPath: name, height: height, width: width, marginLeft: left_margin, marginBottom: bottom_margin, marginRight: right_margin, marginTop: top_margin);

                // Download file Cloud storage
                var downloadResponse = pdfApi.DownloadFile("aspose-output-container" + " / " + "Team3WorkOrderTemplate.pdf");
                var fileStream = System.IO.File.Create("C:/Temp/Team3WorkOrderTemplate.pdf");
                downloadResponse.CopyTo(fileStream);
                fileStream.Close(); 

            }

        }

        //// Load existing PDF document
        var pdfDocument = new Aspose.Pdf.Document(docFilePath);  
        if (!string.IsNullOrEmpty(header))
        {
            var headerFragment = new HtmlFragment(header);

            foreach (Aspose.Pdf.Page page in pdfDocument.Pages)
            {
                page.Header = new Aspose.Pdf.HeaderFooter
                {
                    Margin = new Aspose.Pdf.MarginInfo(0, 0, 0, 0),
                    Paragraphs = { headerFragment }
                };
            }

        }
        else
        {
            string alignmentString = manifest.Input.PageSetup.HeaderText.Alignment;
            // Create header
            Aspose.Pdf.TextStamp headerStamp = new Aspose.Pdf.TextStamp(manifest.Input.PageSetup.HeaderText.Text);
            headerStamp.TopMargin = 50;
            headerStamp.LeftMargin = 50;
            headerStamp.RightMargin = 50;
            headerStamp.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Top;
            headerStamp.TextState.FontSize = manifest.Input.PageSetup.HeaderText.FontSize;
            Aspose.Pdf.Text.Font font = FontRepository.FindFont(manifest.Input.PageSetup.HeaderText.Font);
            headerStamp.TextState.Font = font;


            Aspose.Pdf.HorizontalAlignment alignment = GetAsposeAlignmentFromManifest(manifest.Input.PageSetup.HeaderText.Alignment);
            headerStamp.HorizontalAlignment = alignment;

            if (Enum.TryParse(alignmentString, out Aspose.Pdf.HorizontalAlignment alignmentEnum))
            {
                // Use alignmentEnum in the assignment
                headerStamp.HorizontalAlignment = alignmentEnum;
            }
            else
            {

            }

            // Add header to all pages
            foreach (Aspose.Pdf.Page page in pdfDocument.Pages)
            {
                page.AddStamp(headerStamp);
            }
        }

        if (!string.IsNullOrEmpty(footer))
        {
            //  Add Footer to every page 
            var footerFragment = new HtmlFragment(footer);

            foreach (Aspose.Pdf.Page page in pdfDocument.Pages)
            {
                // Create a TextFragment to display the page number
                TextFragment pageText = new TextFragment($"{page.Number} / {pdfDocument.Pages.Count}");
                pageText.TextState.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Right; // Set alignment 

                // Set the margin for footerFragment
                footerFragment.Margin = new Aspose.Pdf.MarginInfo(0, 0, 0, 0); // Adjust the bottom margin as needed 
                                                                               // Create a HeaderFooter and add the pageText
                page.Footer = new Aspose.Pdf.HeaderFooter
                {
                    Margin = new Aspose.Pdf.MarginInfo(0, 0, 10, 0),
                    Paragraphs = { pageText, footerFragment }
                };
            }
        }
        else
        {
            string alignmentString = manifest.Input.PageSetup.FooterText.Alignment;

            // Create footer
            Aspose.Pdf.TextStamp textStamp = new Aspose.Pdf.TextStamp(manifest.Input.PageSetup.FooterText.Text.ToString());
            textStamp.BottomMargin = 10;

            // Get alignment from manifest and set it
            Aspose.Pdf.HorizontalAlignment alignment = GetAsposeAlignmentFromManifest(alignmentString);
            textStamp.HorizontalAlignment = alignment;

            Aspose.Pdf.VerticalAlignment verticalAlignment = Aspose.Pdf.VerticalAlignment.Bottom;
            textStamp.VerticalAlignment = verticalAlignment;

            Aspose.Pdf.Text.Font font = FontRepository.FindFont(manifest.Input.PageSetup.HeaderText.Font);
            textStamp.TextState.Font = font;
            textStamp.TextState.FontSize = manifest.Input.PageSetup.FooterText.FontSize;

            // Add footer and footer image to all pages
            foreach (Aspose.Pdf.Page page in pdfDocument.Pages)
            {
                page.AddStamp(textStamp);
            }


        // Create page number stamp
        Aspose.Pdf.PageNumberStamp pageNumberStamp = new Aspose.Pdf.PageNumberStamp();
        pageNumberStamp.Background = false;
        pageNumberStamp.Format = "# / " + pdfDocument.Pages.Count;
        pageNumberStamp.TopMargin = 10;
        pageNumberStamp.RightMargin = 10;
        pageNumberStamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Right;
        pageNumberStamp.StartingNumber = 1;
        //  pageNumberStamp.TextState.Font = FontRepository.FindFont("Arial");
        //  pageNumberStamp.TextState.FontSize = 11.0F; 

        // Add page number stamp to all pages
        foreach (Aspose.Pdf.Page page in pdfDocument.Pages)
        {
            page.AddStamp(pageNumberStamp);
        }
    }

        //   Set Page Size
        if (!string.IsNullOrEmpty(manifest.Input.PageSetup.Size))
        {
            switch (manifest.Input.PageSetup.Size.ToLower())
            {
                case "a4":
                    pdfDocument.Pages[1].SetPageSize(PageSize.A4.Width, PageSize.A4.Height);
                    break;
            }

}

        // Set Page Orientation for all pages
        if (!string.IsNullOrEmpty(manifest.Input.PageSetup.Orientation))
        {
            Aspose.Pdf.Rotation rotation = Aspose.Pdf.Rotation.None;

            switch (manifest.Input.PageSetup.Orientation.ToLower())
            {
                case "portrait":
                    rotation = Aspose.Pdf.Rotation.None;
                    break;
                case "landscape":
                    rotation = Aspose.Pdf.Rotation.on270;
                    break;
                // Add more cases for other orientations as needed
                default:
                    // Default to portrait
                    rotation = Aspose.Pdf.Rotation.None;
                    break;
            }

            // Loop through all pages and set the orientation
            foreach (Aspose.Pdf.Page page in pdfDocument.Pages)
            {

                page.Rotate = rotation;
            }
        }


        pdfDocument.Save(manifest.Output.OutputFileName);
        byte[] fileBytes = File.ReadAllBytes(manifest.Output.OutputFileName);

        if (File.Exists(htmlFilePath))
        {
            File.Delete(htmlFilePath);
        }

        if (File.Exists(manifest.Output.OutputFileName))
        {
            File.Delete(manifest.Output.OutputFileName);
        }
        if (File.Exists(htmlHeaderPath))
        {
            File.Delete(htmlHeaderPath);
        }

        if (File.Exists(manifest.Template.HeaderTemplate))
        {
            File.Delete(manifest.Template.HeaderTemplate);
        }
        if (File.Exists(htmlFooterPath))
        {
            File.Delete(htmlFooterPath);
        }

        if (File.Exists(manifest.Template.FooterTemplate))
        {
            File.Delete(manifest.Template.FooterTemplate);
        }

        return fileBytes;

    }

I am trying to to get html file from azure to local. (content,header and footer) then i am trying to change it (page number footer … ) then save it to azure outputcontainer

@niku2023

I’m afraid I’m unable to test your code due to missing references. Please create a simple project only to convert and download file from cloud storage using the Aspose.PDF Cloud API to localize the issue. If the issue persists, then please share that working project with us for investigation.