简体   繁体   English

如何解决此PDF生成问题?

[英]How can I troubleshoot this PDF generation?

Employee moved on and left me with this code that was once working to generate PDFs. 员工继续前进,并留下了我曾经用来生成PDF的这段代码。 I haven't had any luck trying to debug - with breakpoints or even console.logs - the script listed here at the bottom; 我没有尝试进行调试的运气-带有断点,甚至没有console.logs-底部列出的脚本; is there a way to search the huge list of loaded scripts in Visual Studio? 有没有一种方法可以在Visual Studio中搜索大量已加载的脚本?

C# error: C#错误:

{System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.IO.IOException: The server returned an invalid or unrecognized response.
   at System.Net.Http.HttpConnection.FillAsync()

Client Side error: (is this because the server never returns anything?) 客户端错误:(这是因为服务器从不返回任何内容吗?)

ERROR Error: Uncaught (in promise): DataCloneError: Failed to execute 'postMessage' on 'Worker': TypeError: Failed to fetch could not be cloned.
Error: Failed to execute 'postMessage' on 'Worker': TypeError: Failed to fetch could not be cloned.
    at MessageHandler.postMessage (pdf.js:12334)
    at sendStreamRequest (pdf.js:12151)
    at Object.error (pdf.js:12194)
    at eval (pdf.js:8419)
    at ZoneDelegate.invoke (zone.js:392)

Controller method 控制器方式

public async Task<IActionResult> Generate(string id)
{
    try
    {
        var stream = await _reportService.GenerateReportAsync(id);
        return new FileStreamResult(stream, "application/pdf");
    }
    catch(Exception ex)
    {
        throw;
    }            
}

Service method: 服务方式:

public async Task<Stream> GenerateReportAsync(string id)
{
    return await Services.InvokeAsync<Stream>("./Node/generate-pdf.js", Configuration.Url, id, new { format = "A4" });
}

generate-pdf.js: 产生-pdf.js:

const pdf = require('html-pdf');
const puppeteer = require('puppeteer');

module.exports = async function (result, url, id, options) {

const browser = await createBrowser();
const page = await browser.newPage();
const css = await browser.newPage();

await page.goto(`${url}/reports/${id}`, {
    waitUntil: 'networkidle2'
});

await css.goto(`${url}/styles.bundle.css`, {
    waitUntil: 'networkidle2'
});

await page.waitForSelector('.report-loaded');
let cssBody = await css.evaluate(() => `<style>${document.documentElement.innerHTML}</style>`);
let bodyHtml = await page.evaluate(() => document.documentElement.innerHTML);

bodyHtml = bodyHtml.replace('<link href="styles.bundle.css" rel="stylesheet">', cssBody);

browser.close();

pdf.create(cssBody + bodyHtml, options).toStream((error, stream) => stream.pipe(result.stream));
}

async function createBrowser() {
    return await puppeteer.launch({
        args: ['--no-sandbox', '--disable-setuid-sandbox']
    });
}

Looks like the generate-pdf.js script is using "html-pdf". 看起来generate-pdf.js脚本正在使用“ html-pdf”。 This can be found on npm: https://www.npmjs.com/package/html-pdf 可以在npm上找到: https ://www.npmjs.com/package/html-pdf

And it has a github page: https://github.com/marcbachmann/node-html-pdf 它有一个github页面: https : //github.com/marcbachmann/node-html-pdf

So the problem is going to be with the usage of that package, or some kind of bug in it. 因此,问题将出在该软件包的使用或其中的某种错误中。 (well, that's an assumption on my part, I don't know this package at all and have no experience working with it) (嗯,这只是我的一个假设,我一点也不知道这个程序包,也没有使用它的经验)

At this point I'd try to figure out which version of that package is being used, check out the source code and try to find a hint in there. 在这一点上,我将尝试找出正在使用该程序包的哪个版本,查看源代码并尝试在其中找到提示。

This structure seems rather convoluted though. 但是,这种结构看起来有些复杂。 Why not just generate the PDF in the client in the first place, or generate it in the C# code? 为什么不首先在客户端中生成PDF,还是在C#代码中生成PDF? That it was working at some point shouldn't be an argument as you are now noticing this is proving difficult to maintain. 在某个时候它正在工作不应该成为争论,因为您现在注意到这很难维持。

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

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