我如何在我的Vue.js项目中以横向模式打印HTML页面?

huangapple go评论105阅读模式
英文:

How can I print html page in Landscape mode in my Vue.js project

问题

我可以在横向模式下打印HTML页面,如下所示。

但是我无法找到如何在Vue组件页面中以横向模式打印的方法。我在项目中使用Printd组件进行页面打印(https://www.npmjs.com/package/printd)。
即使我设置为横向模式,页面始终以纵向模式显示。

如何以编程方式设置横向模式?

  1. <template>
  2. <div ref="printMe">
  3. <img src="https://www.apple.com/ac/structured-data/images/knowledge_graph_logo.png?201812022340">
  4. </div>
  5. </template>
  6. <script>
  7. import { Printd } from 'printd';
  8. export default {
  9. mounted() {
  10. this.print();
  11. },
  12. methods: {
  13. print() {
  14. const p = new Printd();
  15. p.print(
  16. this.$refs.printMe,
  17. `@media print {
  18. @page {size: A4 landscape;}
  19. }`,
  20. );
  21. },
  22. },
  23. };
  24. </script>
  25. <style scoped>
  26. @media print {
  27. @page {
  28. size: landscape;
  29. }
  30. }
  31. </style>
英文:

I can print HTML page in Landscape mode like below.

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html lang=&quot;en&quot;&gt;
  3. &lt;head&gt;
  4. &lt;meta charset=&quot;UTF-8&quot;&gt;
  5. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width,maximum-scale=1.0&quot;&gt;
  6. &lt;link rel=&quot;icon&quot; type=&quot;image/x-icon&quot; href=&quot;/favicon.ico&quot;&gt;
  7. &lt;style type=&quot;text/css&quot; media=&quot;screen&quot;&gt;&lt;/style&gt;
  8. &lt;style type=&quot;text/css&quot; media=&quot;print&quot;&gt;
  9. /* @page {size:landscape} */
  10. @media print {
  11. @page {size: A4 landscape;}
  12. }
  13. &lt;/style&gt;
  14. &lt;title&gt;Landscape&lt;/title&gt;
  15. &lt;meta name=&quot;description&quot; content=&quot;&quot;&gt;
  16. &lt;meta name=&quot;keywords&quot; content=&quot;&quot;&gt;
  17. &lt;/head&gt;
  18. &lt;body&gt;
  19. &lt;img src=&quot;https://www.apple.com/ac/structured-data/images/knowledge_graph_logo.png?201812022340&quot;&gt;
  20. &lt;/body&gt;
  21. &lt;/html&gt;

But I can't find the way how to print a Vue component page in landscape mode. I'm using Printd component to print page in my project. (https://www.npmjs.com/package/printd)
Even though I set landscape mode, always be set portrait mode.

How can I set landscape mode programmatically?

  1. &lt;template&gt;
  2. &lt;div ref=&quot;printMe&quot;&gt;
  3. &lt;img src=&quot;https://www.apple.com/ac/structured-data/images/knowledge_graph_logo.png?201812022340&quot;&gt;
  4. &lt;/div&gt;
  5. &lt;/template&gt;
  6. &lt;script&gt;
  7. import { Printd } from &#39;printd&#39;;
  8. export default {
  9. mounted() {
  10. this.print();
  11. },
  12. methods: {
  13. print() {
  14. const p = new Printd();
  15. p.print(
  16. this.$refs.printMe,
  17. `@media print {
  18. @page {size: A4 landscape;}
  19. }`,
  20. );
  21. },
  22. },
  23. };
  24. &lt;/script&gt;
  25. &lt;style scoped&gt;
  26. @media print {
  27. @page {
  28. size: landscape;
  29. }
  30. }
  31. &lt;/style&gt;

答案1

得分: 3

  • 你使用的媒体查询被定义为始终表示整个页面 - 这是它的预期行为。
  • 你不能将页面上的单个元素放入“横向”模式,这仅适用于页面。
  • 因此,你需要手动更改元素的尺寸,使其适应你的需求。请参考下面的样式:
  1. .orientation {
  2. width: 400px; /* 正常宽度 */
  3. }
  4. @media print {
  5. .orientation {
  6. width: 100%; /* 打印宽度 */
  7. }
  8. }
英文:
  • The media query you use is defined to always mean the full page -
    that’s what its meant to do.

  • You can’t put individual elements on a page into “landscape” mode, that’s only
    possible for pages.

  • So you will have to change the dimensions of the element manually to those that
    fit your needs. See the below style

    1. . orientation {
    2. width: 400px /* normal width */
    3. }
    4. @media print {
    5. .orientation {
    6. width: 100% /* print width */
    7. }
    8. }

huangapple
  • 本文由 发表于 2020年1月3日 13:20:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/59573552.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定