简体   繁体   English

在内存流中打开PDF时可以打印PDF吗?

[英]It's posible print PDF when PDF is open in memorystream?

Its posible open print dialog when is open in memorystram? 它可能的打开打印对话框何时在memorystram中打开? Actually I save the pdf in "~/App_Data/Rep_PDF/" and after show a pdf in an iframe but I cant do it because I recive a message : 实际上,我将pdf保存在“〜/ App_Data / Rep_PDF /”中,并在iframe中显示了pdf后,但由于我收到一条消息而无法这样做:

"Not allowed to load local resource: file:///C:/Users/andres.martinez/Documents/TFS%20Monterrey%20Gob/InterfaceMonterrey%20PROD/Interface%20Monterrey%20PROD/InterfaceMonterrey/App_Data/Rep_PDF/Copia_C_Administrativa%2030-04-2019.pdf" “不允许加载本地资源:file:/// C:/Users/andres.martinez/Documents/TFS%20Monterrey%20Gob/InterfaceMonterrey%20PROD/Interface%20Monterrey%20PROD/InterfaceMonterrey/App_Data/Rep_PDF/Copia_C_Administrativa%2030- 04-2019.pdf”

Could you help me How to print a pdf in asp mvc? 您能帮我如何在ASP MVC中打印pdf?

this is part of my code: 这是我的代码的一部分:

 public ActionResult ImprimeReporte()
    {
        //Indicamos donde vamos a guardar el documento
        string directorioRaiz = "~/App_Data/Rep_PDF/";
        string NombreArchivoPDF = "C_Administrativa " + DateTime.Today.ToString("dd-MM-yyyy") + ".pdf";
        string path_Original=Server.MapPath(directorioRaiz+NombreArchivoPDF);
        string path_Copia = Server.MapPath(directorioRaiz + "Copia_"+NombreArchivoPDF);

        if (System.IO.File.Exists(path_Original))
        {
            //SI EXISTE EL ARCHIVO EN LA CARPETA, LO MANDA A IMPRIMIR
            Inicia_PrintScript(path_Original, path_Copia);
            ViewData["path_Copia"] = path_Copia;
            //Elimina los archivos , despues de que se imprimio
            // System.IO.File.Delete(path_Original);
            //System.IO.File.Delete(path_Copia);
        }
        else
        {
            //SI NO EXISTE MANDA LLAMAR AL METODO PARA DESCARGAR EL ARCHIVO Y DESPUES IMPRIMIRLO
            Genera_Pdf_Administrativo();
            Inicia_PrintScript(path_Original, path_Copia);
            ViewData["path_Copia"] = path_Copia;
            //Elimina los archivos , despues de que se imprimio
            //System.IO.File.Delete(path_Original);
            //System.IO.File.Delete(path_Copia);
        }
        return View();
    }

second part 第二部分

public static void Inicia_PrintScript(string Original, string Copia)
    {
        PdfReader reader = new PdfReader(Original);
        PdfStamper stamper = new PdfStamper(reader, new FileStream(Copia, FileMode.Create));
        AcroFields fields = stamper.AcroFields;
        stamper.JavaScript = "this.print(true);\r";
        stamper.FormFlattening = true;
        stamper.Close();
        reader.Close();

View 视图

 <iframe src="@ViewData["path_Copia"].ToString()" id="myFrame" frameborder="0" style="border:0;" width="700" height="300"></iframe> 

Well, it's possible to open a PDF with print dialog in another tab. 好了,可以在另一个选项卡中打开带有打印对话框的PDF。

In your controller you need to get the file in memory just to return it, or generate the file on fly. 在您的控制器中,您需要将文件保存在内存中只是为了将其返回,或即时生成文件。

public FileResult PDFToReturn()
{
    string filePath = Server.MapPath("Path_to_PDF");
    return File(System.IO.File.ReadAllBytes(filePath), System.Web.MimeMapping.GetMimeMapping(filePath));
}

javascript does the magic. javascript发挥了魔力。

function printPDF() {
    let link = 'javascript:w=window.open("/Controller/PDFToReturn"); w.print();';
    location.href = link;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM