Single page PDF in puppeetersharp












1















I have tried to convert a web page to single page pdf, but there is no support for this. Is there any workaround to achieve this requirement?



I have already tried by setting the pdf page size from html content size. But it is not working as expected for the all the webpage. I have get the html content size using EvaluateExpressionAsync. Below are the code snippets i have tried to achieve my requirement, but not working for all the web pages (mostly responsive webpages).



int height = await page.EvaluateExpressionAsync("document.body.clientHeight");


and



dynamic metrics = await Client.SendAsync("Page.getLayoutMetrics").ConfigureAwait(false); 

var width = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(metrics.contentSize.width.Value)));
var height = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(metrics.contentSize.height.Value)));


I have set the above height and width to pdf page size like the screenshot implementation, but the does not work for all the web page. But it is working properly in Screenshot implementation. Can you help me to achieve this?










share|improve this question

























  • Do you need to zoom the page so it fits in one PDF page (e.g. A4)? or do you need the pdf to create huge page size?

    – hardkoded
    Nov 22 '18 at 13:27











  • I need to increase the pdf page height based on the webpage height.

    – john
    Nov 23 '18 at 3:58
















1















I have tried to convert a web page to single page pdf, but there is no support for this. Is there any workaround to achieve this requirement?



I have already tried by setting the pdf page size from html content size. But it is not working as expected for the all the webpage. I have get the html content size using EvaluateExpressionAsync. Below are the code snippets i have tried to achieve my requirement, but not working for all the web pages (mostly responsive webpages).



int height = await page.EvaluateExpressionAsync("document.body.clientHeight");


and



dynamic metrics = await Client.SendAsync("Page.getLayoutMetrics").ConfigureAwait(false); 

var width = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(metrics.contentSize.width.Value)));
var height = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(metrics.contentSize.height.Value)));


I have set the above height and width to pdf page size like the screenshot implementation, but the does not work for all the web page. But it is working properly in Screenshot implementation. Can you help me to achieve this?










share|improve this question

























  • Do you need to zoom the page so it fits in one PDF page (e.g. A4)? or do you need the pdf to create huge page size?

    – hardkoded
    Nov 22 '18 at 13:27











  • I need to increase the pdf page height based on the webpage height.

    – john
    Nov 23 '18 at 3:58














1












1








1








I have tried to convert a web page to single page pdf, but there is no support for this. Is there any workaround to achieve this requirement?



I have already tried by setting the pdf page size from html content size. But it is not working as expected for the all the webpage. I have get the html content size using EvaluateExpressionAsync. Below are the code snippets i have tried to achieve my requirement, but not working for all the web pages (mostly responsive webpages).



int height = await page.EvaluateExpressionAsync("document.body.clientHeight");


and



dynamic metrics = await Client.SendAsync("Page.getLayoutMetrics").ConfigureAwait(false); 

var width = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(metrics.contentSize.width.Value)));
var height = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(metrics.contentSize.height.Value)));


I have set the above height and width to pdf page size like the screenshot implementation, but the does not work for all the web page. But it is working properly in Screenshot implementation. Can you help me to achieve this?










share|improve this question
















I have tried to convert a web page to single page pdf, but there is no support for this. Is there any workaround to achieve this requirement?



I have already tried by setting the pdf page size from html content size. But it is not working as expected for the all the webpage. I have get the html content size using EvaluateExpressionAsync. Below are the code snippets i have tried to achieve my requirement, but not working for all the web pages (mostly responsive webpages).



int height = await page.EvaluateExpressionAsync("document.body.clientHeight");


and



dynamic metrics = await Client.SendAsync("Page.getLayoutMetrics").ConfigureAwait(false); 

var width = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(metrics.contentSize.width.Value)));
var height = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(metrics.contentSize.height.Value)));


I have set the above height and width to pdf page size like the screenshot implementation, but the does not work for all the web page. But it is working properly in Screenshot implementation. Can you help me to achieve this?







c# google-chrome-headless puppeteer-sharp






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 12:31









hardkoded

5,53321928




5,53321928










asked Nov 22 '18 at 4:01









johnjohn

173




173













  • Do you need to zoom the page so it fits in one PDF page (e.g. A4)? or do you need the pdf to create huge page size?

    – hardkoded
    Nov 22 '18 at 13:27











  • I need to increase the pdf page height based on the webpage height.

    – john
    Nov 23 '18 at 3:58



















  • Do you need to zoom the page so it fits in one PDF page (e.g. A4)? or do you need the pdf to create huge page size?

    – hardkoded
    Nov 22 '18 at 13:27











  • I need to increase the pdf page height based on the webpage height.

    – john
    Nov 23 '18 at 3:58

















Do you need to zoom the page so it fits in one PDF page (e.g. A4)? or do you need the pdf to create huge page size?

– hardkoded
Nov 22 '18 at 13:27





Do you need to zoom the page so it fits in one PDF page (e.g. A4)? or do you need the pdf to create huge page size?

– hardkoded
Nov 22 '18 at 13:27













I need to increase the pdf page height based on the webpage height.

– john
Nov 23 '18 at 3:58





I need to increase the pdf page height based on the webpage height.

– john
Nov 23 '18 at 3:58












1 Answer
1






active

oldest

votes


















1














You can use the PdfOptions.Height and PdfOptions.Width.You might find that it's not pixel perfect but that's more on the Chromium's side than on Puppeteer's.



await page.SetViewportAsync(new ViewPortOptions()
{
Height = 1024,
Width = 1280
});
await page.GoToAsync("https://github.com/kblok/puppeteer-sharp/");
int height = await page.EvaluateExpressionAsync<int>("document.body.offsetHeight");
int width = await page.EvaluateExpressionAsync<int>("document.body.offsetWidth");
await page.PdfAsync(Path.Combine(Directory.GetCurrentDirectory(), "test.pdf"), new PdfOptions
{
Width = width.ToString() + "px",
Height = height.ToString() + "px",
});





share|improve this answer
























  • I have tried the provided example code snippet and it is working properly for some website and it is not working as expected for responsive web pages. For example please convert this link, it will generate multiple page pdf instead of single page. URL = stackoverflow.com/questions/14001483/…

    – john
    Nov 26 '18 at 10:06











  • Yeah, but that's more a Chorium issue than a Puppeteer one.

    – hardkoded
    Nov 26 '18 at 12:01











  • okay, is there any upstream bug report in chromium for this?

    – john
    Nov 26 '18 at 12:20








  • 1





    I think if you can file this issue on Google`s Puppeteer they can give you some timelines or where to report it

    – hardkoded
    Nov 26 '18 at 12:26











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423691%2fsingle-page-pdf-in-puppeetersharp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You can use the PdfOptions.Height and PdfOptions.Width.You might find that it's not pixel perfect but that's more on the Chromium's side than on Puppeteer's.



await page.SetViewportAsync(new ViewPortOptions()
{
Height = 1024,
Width = 1280
});
await page.GoToAsync("https://github.com/kblok/puppeteer-sharp/");
int height = await page.EvaluateExpressionAsync<int>("document.body.offsetHeight");
int width = await page.EvaluateExpressionAsync<int>("document.body.offsetWidth");
await page.PdfAsync(Path.Combine(Directory.GetCurrentDirectory(), "test.pdf"), new PdfOptions
{
Width = width.ToString() + "px",
Height = height.ToString() + "px",
});





share|improve this answer
























  • I have tried the provided example code snippet and it is working properly for some website and it is not working as expected for responsive web pages. For example please convert this link, it will generate multiple page pdf instead of single page. URL = stackoverflow.com/questions/14001483/…

    – john
    Nov 26 '18 at 10:06











  • Yeah, but that's more a Chorium issue than a Puppeteer one.

    – hardkoded
    Nov 26 '18 at 12:01











  • okay, is there any upstream bug report in chromium for this?

    – john
    Nov 26 '18 at 12:20








  • 1





    I think if you can file this issue on Google`s Puppeteer they can give you some timelines or where to report it

    – hardkoded
    Nov 26 '18 at 12:26
















1














You can use the PdfOptions.Height and PdfOptions.Width.You might find that it's not pixel perfect but that's more on the Chromium's side than on Puppeteer's.



await page.SetViewportAsync(new ViewPortOptions()
{
Height = 1024,
Width = 1280
});
await page.GoToAsync("https://github.com/kblok/puppeteer-sharp/");
int height = await page.EvaluateExpressionAsync<int>("document.body.offsetHeight");
int width = await page.EvaluateExpressionAsync<int>("document.body.offsetWidth");
await page.PdfAsync(Path.Combine(Directory.GetCurrentDirectory(), "test.pdf"), new PdfOptions
{
Width = width.ToString() + "px",
Height = height.ToString() + "px",
});





share|improve this answer
























  • I have tried the provided example code snippet and it is working properly for some website and it is not working as expected for responsive web pages. For example please convert this link, it will generate multiple page pdf instead of single page. URL = stackoverflow.com/questions/14001483/…

    – john
    Nov 26 '18 at 10:06











  • Yeah, but that's more a Chorium issue than a Puppeteer one.

    – hardkoded
    Nov 26 '18 at 12:01











  • okay, is there any upstream bug report in chromium for this?

    – john
    Nov 26 '18 at 12:20








  • 1





    I think if you can file this issue on Google`s Puppeteer they can give you some timelines or where to report it

    – hardkoded
    Nov 26 '18 at 12:26














1












1








1







You can use the PdfOptions.Height and PdfOptions.Width.You might find that it's not pixel perfect but that's more on the Chromium's side than on Puppeteer's.



await page.SetViewportAsync(new ViewPortOptions()
{
Height = 1024,
Width = 1280
});
await page.GoToAsync("https://github.com/kblok/puppeteer-sharp/");
int height = await page.EvaluateExpressionAsync<int>("document.body.offsetHeight");
int width = await page.EvaluateExpressionAsync<int>("document.body.offsetWidth");
await page.PdfAsync(Path.Combine(Directory.GetCurrentDirectory(), "test.pdf"), new PdfOptions
{
Width = width.ToString() + "px",
Height = height.ToString() + "px",
});





share|improve this answer













You can use the PdfOptions.Height and PdfOptions.Width.You might find that it's not pixel perfect but that's more on the Chromium's side than on Puppeteer's.



await page.SetViewportAsync(new ViewPortOptions()
{
Height = 1024,
Width = 1280
});
await page.GoToAsync("https://github.com/kblok/puppeteer-sharp/");
int height = await page.EvaluateExpressionAsync<int>("document.body.offsetHeight");
int width = await page.EvaluateExpressionAsync<int>("document.body.offsetWidth");
await page.PdfAsync(Path.Combine(Directory.GetCurrentDirectory(), "test.pdf"), new PdfOptions
{
Width = width.ToString() + "px",
Height = height.ToString() + "px",
});






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 10:48









hardkodedhardkoded

5,53321928




5,53321928













  • I have tried the provided example code snippet and it is working properly for some website and it is not working as expected for responsive web pages. For example please convert this link, it will generate multiple page pdf instead of single page. URL = stackoverflow.com/questions/14001483/…

    – john
    Nov 26 '18 at 10:06











  • Yeah, but that's more a Chorium issue than a Puppeteer one.

    – hardkoded
    Nov 26 '18 at 12:01











  • okay, is there any upstream bug report in chromium for this?

    – john
    Nov 26 '18 at 12:20








  • 1





    I think if you can file this issue on Google`s Puppeteer they can give you some timelines or where to report it

    – hardkoded
    Nov 26 '18 at 12:26



















  • I have tried the provided example code snippet and it is working properly for some website and it is not working as expected for responsive web pages. For example please convert this link, it will generate multiple page pdf instead of single page. URL = stackoverflow.com/questions/14001483/…

    – john
    Nov 26 '18 at 10:06











  • Yeah, but that's more a Chorium issue than a Puppeteer one.

    – hardkoded
    Nov 26 '18 at 12:01











  • okay, is there any upstream bug report in chromium for this?

    – john
    Nov 26 '18 at 12:20








  • 1





    I think if you can file this issue on Google`s Puppeteer they can give you some timelines or where to report it

    – hardkoded
    Nov 26 '18 at 12:26

















I have tried the provided example code snippet and it is working properly for some website and it is not working as expected for responsive web pages. For example please convert this link, it will generate multiple page pdf instead of single page. URL = stackoverflow.com/questions/14001483/…

– john
Nov 26 '18 at 10:06





I have tried the provided example code snippet and it is working properly for some website and it is not working as expected for responsive web pages. For example please convert this link, it will generate multiple page pdf instead of single page. URL = stackoverflow.com/questions/14001483/…

– john
Nov 26 '18 at 10:06













Yeah, but that's more a Chorium issue than a Puppeteer one.

– hardkoded
Nov 26 '18 at 12:01





Yeah, but that's more a Chorium issue than a Puppeteer one.

– hardkoded
Nov 26 '18 at 12:01













okay, is there any upstream bug report in chromium for this?

– john
Nov 26 '18 at 12:20







okay, is there any upstream bug report in chromium for this?

– john
Nov 26 '18 at 12:20






1




1





I think if you can file this issue on Google`s Puppeteer they can give you some timelines or where to report it

– hardkoded
Nov 26 '18 at 12:26





I think if you can file this issue on Google`s Puppeteer they can give you some timelines or where to report it

– hardkoded
Nov 26 '18 at 12:26




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423691%2fsingle-page-pdf-in-puppeetersharp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy