Ver Mensaje Individual
  #8 (permalink)  
Antiguo 19/11/2014, 22:57
Avatar de Grost
Grost
 
Fecha de Ingreso: enero-2014
Ubicación: Guatemala
Mensajes: 25
Antigüedad: 10 años, 4 meses
Puntos: 1
Respuesta: Generar pdf desde aspx

Yo utilizo el control de google: wkhtmltopdf
este me ha servido muy bien.

Este es el link para descargarlo es gratuito: http://wkhtmltopdf.org/downloads.html

Este es el código que yo utilizo en una clase para exportar mi página web a PDF

Este es el código del botón de la página desde ejecuto la clase que contiene la funcionalidad de la exportación a PDF de la página ASPX

Código:
 protected void btnExportarPDF_Click(object sender, EventArgs e)
    {
        string URL_EXPORT_PDF = ConfigurationManager.AppSettings["URL_EXPORTAR_PDF"];
        exportarPDF pdf = new exportarPDF();
        byte[] fileContent = pdf.GeneratePDFFile(URL_EXPORT_PDF + "?parametro=" + hdfDataKey.Value.ToString(), "C:\\PDF", Server.MapPath("~/PDFConverter"));


        if (fileContent != null)
        {
            Response.Clear();
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-length", fileContent.Length.ToString());
            if (Request.QueryString["attachment"] == null)
            {
                Response.AddHeader("content-disposition", "attachment; filename = detalleEvaluacion.pdf");
            }
            else
            {
                Response.AddHeader("content-disposition", "inline; filename = detalleEvaluacion.pdf");
            }


            Response.BinaryWrite(fileContent);
            Response.End();
        }
    }


Esta es la clase que utilizo para ejecutar la exportación a PDF y abrir el mismo archivo en el navegador.

Código:
    public byte[] GeneratePDFFile(string url, string salida, string ejecutable)
    {
        //url of the page we would wanto convert.
        //we dont need to hard code this URL if we want to export current page
        //we could prepare the URL by using HTTPRequest object oo
        //string url = @"http://localhost:54630/PageExportPDF.aspx";
        //string url = Request.Url.AbsoluteUri;

        //output PDF file Path
        string filepath = Path.Combine(salida, "NOMBRE_REPORTE.pdf");

        //variable to store pdf file content
        byte[] fileContent = null;

        System.Diagnostics.Process process = new System.Diagnostics.Process();
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.CreateNoWindow = true;

        //set the executable location
        process.StartInfo.FileName = Path.Combine(ejecutable, "wkhtmltopdf.exe");
        //set the arguments to the exectuable
        // wkhtmltopdf [OPTIONS]... <input fileContent> [More input fileContents] <output fileContent>
        process.StartInfo.Arguments = url + " " + filepath;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.RedirectStandardError = true;
        process.StartInfo.RedirectStandardInput = true;
        //run the executable
        process.Start();

        //wait until the conversion is done
        process.WaitForExit();

        // read the exit code, close process    
        int returnCode = process.ExitCode;
        process.Close();

        //initialize the filestream with filepath
        FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
        fileContent = new byte[(int)fs.Length];

        //read the content
        fs.Read(fileContent, 0, (int)fs.Length);

        //close the stream
        fs.Close();

        return fileContent;

    }
Espero te pueda servir y si tienes alguna duda puedes dejarme un mensaje o escribir en el historial de este post.